Search code examples
phpsimple-html-domdomparser

Simple HTML DOM parser not updating


I'm using the simple HTML DOM parser for my own template system and found a problem.

Here's my markup:

<div class=content>
    <div class=navigation></div>
</div>

I'm replacing the div.navigation with own content like:

$navi= $dom->find("div.navigation",0);
$navi->outertext = "<a class=aNavi>click me!</a>";

works nicely - i can echo it but the problem is - before echoing i still want to access/manipulate that link with the parser, but the parser won't find it.

$link = $dom->find("a.aNavi");

will return null :(

Seems like the parser needs to be refreshed/updated after changing the outertext - any ideas if it's possible?


Solution

  • I don't see any createElement-like method in the API reference, which means either the documentation is incomplete or you're using the wrong tool for the job.

    I suggest using DOMDocument, and the DOMDocument::createElement() method. However, if you're dead set on using Simple HTML DOM Parser, you could try this hack:

    $navi = $dom->find('div.navigation', 0);
    $navi->outertext = '<a class="aNavi">click me!</a>';
    $dom = $dom->save();
    $dom = str_get_html($dom);
    $link = $dom->find('a.aNavi');