php-html-parser icon indicating copy to clipboard operation
php-html-parser copied to clipboard

How to replace a DomElement with a Textnode?

Open do-web opened this issue 4 years ago • 3 comments

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>

do-web avatar Feb 22 '20 11:02 do-web

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();

WebMechanic avatar Mar 21 '20 09:03 WebMechanic

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.

jarkkouro avatar Mar 26 '20 14:03 jarkkouro

Ya, I am working on improving the documentation. As much documentation as we do have it has gotten bigger over time.

paquettg avatar May 12 '20 12:05 paquettg