警告: DOMDocument::loadHTML(): htmlParseEntityRef: 期望在实体中出现 ';'

php
2022-08-30 07:44:23
$html = file_get_contents("http://www.somesite.com/");

$dom = new DOMDocument();
$dom->loadHTML($html);

echo $dom;

抛出

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,
Catchable fatal error: Object of class DOMDocument could not be converted to string in test.php on line 10

答案 1

要取消警告,可以使用libxml_use_internal_errors(true)

// create new DOMDocument
$document = new \DOMDocument('1.0', 'UTF-8');

// set error level
$internalErrors = libxml_use_internal_errors(true);

// load HTML
$document->loadHTML($html);

// Restore error level
libxml_use_internal_errors($internalErrors);

答案 2

我敢打赌,如果你查看源代码,你会发现尚未转换为HTML的特殊字符。也许是这样的:http://www.somesite.com/

<a href="/script.php?foo=bar&hello=world">link</a>

应该是

<a href="/script.php?foo=bar&amp;hello=world">link</a>

推荐