Menu

#146 Replacing not work for all elements

closed
None
2018-12-06
2015-04-08
Artem
No

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="" />

Discussion

  • LogMANOriginal

    LogMANOriginal - 2018-12-06

    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.

    foreach ($dom->find('a') as $elem) {
        $elem->outertext = $elem->innertext;
    }
    

    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).

    foreach ($dom->find('img') as $elem) {
        $elem->src = 'newpic.jpg';
    }
    

    Here you replace the src of all img elements. This code works as expected, but because the anchors above are considered static text, it doesn't affect the img element within.

    What you want to do requires you to do it the other way around:

    <?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('img') as $elem) {
        $elem->src = 'newpic.jpg';
    }
    
    foreach ($dom->find('a') as $elem) {
        $elem->outertext = $elem->innertext;
    }
    
    echo $dom->save();
    

    The output of this code is this:
    my link <img src="newpic.jpg" alt="" /> <img src="newpic.jpg" alt="" />

     
  • LogMANOriginal

    LogMANOriginal - 2018-12-06
    • status: open --> closed
    • assigned_to: LogMANOriginal
     

Log in to post a comment.