yigitozmen
yigitozmen

Reputation: 957

DOMDocument->saveHTML isn't working

An api returns me couple of html code (only part of the body, not full html) and i want to change all images src's with others.

I get and set attributes then if i echo it in foreach loop i see old and new value but when i try to save it with saveHTML then dump the full html block which is returned from api, i don't see replaced paths.

    $page = json_decode($page);
    $page = (array) $page->rows;
    $page =  ($page[0]->_->content);


    $dom = new \DOMDocument();
    $dom->loadHTML($page);
    $tag = $dom->getElementsByTagName('img');
    foreach($tag as $t)
    {
        echo $t->getAttribute('src').'<br'>; //showing old src
        $t->setAttribute('src', 'bla');
        echo $t->getAttribute('src').'<br'>; //showing new src
    }

    $dom->saveHTML();
    var_dump($page); //nothing is changed

Upvotes: 0

Views: 657

Answers (1)

codtex
codtex

Reputation: 6548

My_ friend this is not how it works.

You should have your edited HTML in the result of saveHTML() so:

$editedHtml = $dom->saveHTML()
var_dump($editedHtml);

Now you should see your changed HTML.

Explanation is that $page is completely different object that has nothing to do with $dom object.

Cheers!

Upvotes: 2

Related Questions