Wednesday, 15 May 2013

php regex expression to get img srcs with exceptions -



php regex expression to get img srcs with exceptions -

how write regex look gets img tags, , within them, gets "src" value, ignoring imgs tags has given class? let's srcs of img tags don't have "dontgetme" assigned classes (but may still have other classes)

i.e.

<img src="teste1.jpg" class="blueclass brightclass dontgetme" /> <img src="teste2.jpg" class="blueclass" /> <img src="teste3.jpg" class="dontgetme" /> <img src="teste4.jpg" />

on example, regex should teste2.jpg , teste4.jpg.

the regex got far next (which gets imgs src values regardless of presence of "dontgetme" class):

((?:\<img).*)(src)

! regex used on php script, has run succesfully on "http://www.phpliveregex.com".

edit: regex used in next php function: totally agree regex doesn't seems clear , guaranteed way it, still, lack of php knowledge ties me technology.

function advanced_lazyload($buffer) { (...) $pattern = '(regex look goes here)'; $buffer = preg_replace($pattern, "$1 src='temp.gif' imageholder", $buffer); homecoming $buffer; }

dont utilize regex parsing html. task xml parser.

the recommended way utilize xpath this.

$doc = new domdocument(); $doc->loadhtml($html); $dox = new domxpath($doc); $elements = $dox->query('//img[not(contains(@class, "dontgetme"))]/@src'); foreach($elements $el){ echo $el->nodevalue, "\n"; }

php regex html-parsing

No comments:

Post a Comment