php-html-parser
php-html-parser copied to clipboard
How to replace a DomElement with a Textnode?
Similiar to this questen here:
<html>
<body>
Lorum ipsum <a href="http://google.com">click here</a> dolores lorem.
Lorum ipsum <a href="http://stackoverflow.com">click here too</a> dolores lorem.
</body>
</html>
to this:
<html>
<body>
Lorum ipsum click here dolores lorem.
Lorum ipsum click here dolores lorem.
</body>
</html>
you mean like this?
$html = '<!DOCTYPE html>
<html>
<body>
Lorum ipsum <a href="http://google.com">click here</a> dolores lorem.
Lorum ipsum <a href="http://stackoverflow.com">click here too</a> dolores lorem.
</body>
</html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
// echo $dom->saveHTML();
$body = $dom->getElementsByTagName('body')[0];
// flatten it
$body->nodeValue = $body->textContent;
echo $dom->saveHTML();
Would it be good idea to include these cases to README-file? I just spent couple of hours searching for information, how to replace a tag name or how to change text of h2-tag. Ended up creating new TextNode and adding it as a child. I was thinking, while searching, that there is so many ways to use this great library, but since the class names and examples are not documented, many might just think that this library cannot do all the things that it can do.
Ya, I am working on improving the documentation. As much documentation as we do have it has gotten bigger over time.