Regex in PHP: how to match any A and IMG HTML tag including content -


this question has answer here:

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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -