警告:DOMDocument :: loadHTML():htmlParseEntityRef:期待';' 在实体中,
$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 
	
 为了libxml_use_internal_errors(true)警告,可以使用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); 
 我敢打赌,如果你看看http://www.somesite.com/的来源,你会发现没有被转换成HTML的特殊字符。 也许这样的事情: 
 <a href="/script.php?foo=bar&hello=world">link</a> 
应该
 <a href="/script.php?foo=bar&hello=world">link</a> 
 $dom->@loadHTML($html); 
这是不正确的,用这个代替:
 @$dom->loadHTML($html); 
致命错误的原因是DOMDocument没有__toString()方法,因此不能被回显。
你可能正在寻找
 echo $dom->saveHTML(); 
有两个错误:第二个是因为$ DOM是没有string,但一个对象,因此不能“回声”。 第一个错误是来自loadHTML的警告,由加载的html文档的无效语法引起(可能是一个用作参数分隔符,而不是用&掩盖的实体)。
您可以通过使用错误控制运算符“@”( http://www.php.net/manual/en/language.operators.errorcontrol )调用该函数来忽略并抑制此错误消息(不是错误,只是消息!) 。 php )
 $dom->@loadHTML($html); 
无论echo(需要用print_r还是var_dump替代),如果抛出exception,对象应保持为空:
 DOMNodeList Object ( ) 
解
- 
将 recover设置为true,并将strictErrorChecking为false$content = file_get_contents($url); $doc = new DOMDocument(); $doc->recover = true; $doc->strictErrorChecking = false; $doc->loadHTML($content);
- 
在标记的内容上使用php的实体编码,这是最常见的错误来源。 
取代简单
 $dom->loadHTML($html); 
与更强大的…
 libxml_use_internal_errors(true); if (!$DOM->loadHTML($page)) { $errors=""; foreach (libxml_get_errors() as $error) { $errors.=$error->message."<br/>"; } libxml_clear_errors(); print "libxml errors:<br>$errors"; return; } 
另一个可能的解决scheme是
 $sContent = htmlspecialchars($sHTML); $oDom = new DOMDocument(); $oDom->loadHTML($sContent); echo html_entity_decode($oDom->saveHTML()); 
我知道这是一个古老的问题,但如果你想要修复HTML中格式不正确的'&'符号。 你可以使用类似这样的代码:
 $page = file_get_contents('http://www.example.com'); $page = preg_replace('/\s+/', ' ', trim($page)); fixAmps($page, 0); $dom->loadHTML($page); function fixAmps(&$html, $offset) { $positionAmp = strpos($html, '&', $offset); $positionSemiColumn = strpos($html, ';', $positionAmp+1); $string = substr($html, $positionAmp, $positionSemiColumn-$positionAmp+1); if ($positionAmp !== false) { // If an '&' can be found. if ($positionSemiColumn === false) { // If no ';' can be found. $html = substr_replace($html, '&', $positionAmp, 1); // Replace straight away. } else if (preg_match('/&(#[0-9]+|[AZ|az|0-9]+);/', $string) === 0) { // If a standard escape cannot be found. $html = substr_replace($html, '&', $positionAmp, 1); // This mean we need to escapa the '&' sign. fixAmps($html, $positionAmp+5); // Recursive call from the new position. } else { fixAmps($html, $positionAmp+1); // Recursive call from the new position. } } } 
这并不总是因为页面的内容, 可能是因为URL本身 。
我最近遇到了这个错误,并且在URL的末尾返回了一个回车符。 这个angular色存在的原因是URL分裂的错误。
 $urls_array = explode("\r\n", $urls); 
代替
 $urls_array = explode("\n", $urls);