Regex in PHP: how to match any A and IMG HTML tag including content -
this question has answer here:
- how parse , process html/xml in php? 27 answers
suppose have text bunch (0 or more) of img , , maybe other html tags this:
hello world hello world <a href='ads'>hello</a> bla bla foo bar <img src='' />
i wanna match in regular expression php , img tag. tags should include tag content in match. other tags other , img discarded now.
so result should be:
//match 1 <a href='ads'>hello</a> //match 2 <img src='' />
is there maybe ready solution. should use regex ?
use domdocument
. particular example requires >= 5.3.6:
$content = <<<eom hello world hello world <a href='ads'>hello</a> bla bla foo bar <img src='' /> eom; $doc = new domdocument; $doc->loadhtml($content); $xp = new domxpath($doc); foreach ($xp->query('//a | //img') $node) { echo $doc->savehtml($node); }
output:
<a href="ads">hello</a><img src="">
Comments
Post a Comment