How to workaround PHP advanced html dom's conv

2019-09-03 15:37发布

问题:

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

Despite

http://archive.is/YWKYp#selection-971.0-979.95

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 ÔÇó

回答1:

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.

Installation

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

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

Documentation

For more informations, please visit the package link: PHPHTMLQuery