Autor Zpráva
Martin2
Profil *
Marně se snažim již několik měsíců pochopit regulérní výrazy, tak bych se chtěl zeptat zkušenějších jestli lze z textu, který osahuje HTML tagy, vytáhnout jen všechny IMG tagy do pole za pomoci regulérních výrazů. Mam to zatím jen přes funkce pro práci s řetězci, ale snad by to šlo udělat i jednodušeji.
díky za radu.
bitbit
Profil

<?php
$text = "jak se vede <img src="http://www.libpng.org/pub/png/images/smile.happy.png">obrazek kolok kolo mlznskz";

ereg ("(<img)(.*)(>)", $text, $regs);
echo "".$regs[1]."".$regs[2]."".$regs[3]."";
?>



toto je jen ukázka jak poznat img a vypsat ho. Snad ti to pomůže.
bitbit
Profil
Jo a tady je to kompletní. Našel sem to zde http://cz2.php.net/manual/cs/function.ereg.php . Jen jsem to upravil aby to do toho pole ukládalo i ten tag <img a konecnou >



<?php
/**
Returns an array containing each of the sub-strings from text that
are between openingMarker and closingMarker. The text from
openingMarker and closingMarker are not included in the result.
This function does not support nesting of markers.
*/
function returnSubstrings($text, $openingMarker, $closingMarker) {
$openingMarkerLength = strlen($openingMarker);
$closingMarkerLength = strlen($closingMarker);

$result = array();
$position = 0;
while (($position = strpos($text, $openingMarker, $position)) !== false) {
$position += $openingMarkerLength;
if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) {
$result[] = "".$openingMarker."".substr($text, $position, $closingMarkerPosition - $position)."".$closingMarker."";
$position = $closingMarkerPosition + $closingMarkerLength;
}
}
return $result;
}

// Example:
$exampleText = "<img src="http://www.libpng.org/pub/png/images/smile.happy.png"> a` tous, <img src="http://www.libpng.org/pub/png/images/smile.happy.png"> allez-vous ?";
$result = returnSubstrings($exampleText, "<img", ">");
var_export($result);

// Prints:
// array (
// 0 => '<img src="http://www.libpng.org/pub/png/images/smile.happy.png">',
// 1 => '<img src="http://www.libpng.org/pub/png/images/smile.happy.png">',
// )
?>
Martin2
Profil *
Díky moc, to je přesně to co jsem potřeboval
bitbit
Profil
Není zač :)
Toto téma je uzamčeno. Odpověď nelze zaslat.

0