Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

What HTML parser are you using ?

Hello,

I need to replace some text content inside some tags in an HTML document, I have tried with DOMDocument PHP native library, but it's not convenient for me because it transforms my HTML code by adding the HTML and BODY tags.

And I need to maintain exactly the same tags organization with just replacing the text inside them.

So I'm looking for another HTML parser.

Have you already used these ones ?

https://github.com/paquettg/php-html-parser

https://packagist.org/packages/ammadeuss/laravel-html-dom-parser

Perhaps you know another one ?

I specify that the HTML string is typed inside a form and is not in a file.

Thanks for your help.

Vincent

0 likes
3 replies
click's avatar
click
Best Answer
Level 35

I have tried with DOMDocument PHP native library, but it's not convenient for me because it transforms my HTML code by adding the HTML and BODY tags.

If that is your only problem you can easily remove them.

With laravel's helper this basic thing should work:

$yourHtml = '<html><head></head><body>Hello!</body></html>';
$cleanedHtml = \Str::between($yourHtml, '<body>', '</body>');
1 like
Snapey's avatar

can you not target the elements with regex? Unless its a complete DOM structure you need to parse (and just what was keyed into an edit field), why not treat it as a string?

1 like

Please or to participate in this conversation.