Home Ask Login Register

Developers Planet

Your answer is one click away!

ChrisJJ February 2016

How to workaround PHP advanced html dom's conversion of entities?

How can I workaround advanced_html_dom.php str_get_html's conversion of HTML entities, short of applying htmlentities() on every element content?



The goal of this project is to be a DOM-based drop-in replacement for PHP's simple html dom library.

... If you use file/str_get_html then you don't need to change anything.

I find on

include 'simple_html_dom.php';
$set = str_get_html('<html><title>&nbsp;</title></html>');
echo ($set->find('title',0)->innertext)."\n";  // Expected: &nbsp;  Observed: &nbsp;

changing to advanced HTML DOM gives an incompatible result:

include 'advanced_html_dom.php';
$set = str_get_html('<html><title>&nbsp;</title></html>');
echo ($set->find('title',0)->innertext)."\n";    // Expected: &nbsp;  Observed: -á

This issue is not confined to spaces.

$set = str_get_html('<html><body>&bull;</body></html>'); 
echo $set->find('body',0)->innertext; // Expected $bull; Observed ÔÇó


someone February 2016

You can check my own package PHPHTMLQuery, it helps you to use PHP to select HTML element using most of CSS3 selectors.

the package works with external links and internal html files too.


Open your terminal and browse into your project root folder and run

composer require "abdelilahlbardi/phphtmlquery":"@dev"


For more informations, please visit the package link: PHPHTMLQuery

Post Status

Asked in February 2016
Viewed 3,897 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life