It was expected that this code will remove all links and then replace "src" attribute for all images.
<?php require('simple_html_dom.php'); $html = ' <a href="http://link.com"> my link </a> <a href="http://mysite.com"> <img src="oldpic1.jpg" alt="" /> </a> <img src="oldpic2.jpg" alt="" /> '; $dom = str_get_html($html); foreach ($dom->find('a') as $elem) { $elem->outertext = $elem->innertext; } foreach ($dom->find('img') as $elem) { $elem->src = 'newpic.jpg'; } echo $dom->save();
But it replace src only in the second image:
my link <img src="oldpic1.jpg" alt="" /> <img src="newpic.jpg" alt="" />
This actually works, just not the way you think it does. Let's go through this step-by-step:
$dom = str_get_html($html);
This loads your HTML into memory, parsing all elements into nodes. At this point the parser has an internal representation of your HTML.
Here you replace the outertext of all anchors by their innertext. This effectively replaces the node by static text (because the provided DOM is not parsed again).
Here you replace the
src
of allimg
elements. This code works as expected, but because the anchors above are considered static text, it doesn't affect theimg
element within.What you want to do requires you to do it the other way around:
The output of this code is this:
my link <img src="newpic.jpg" alt="" /> <img src="newpic.jpg" alt="" />