警告:DOMDocument :: loadHTML():htmlParseEntityRef:expecting';'在实体中,

时间:2009-11-06 03:40:27

标签: php

$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

12 个答案:

答案 0 :(得分:137)

要消除警告,您可以使用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);

答案 1 :(得分:89)

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

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

应该是

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

答案 2 :(得分:49)

$dom->@loadHTML($html);

这是不正确的,请改用:

@$dom->loadHTML($html);

答案 3 :(得分:12)

致命错误的原因是DOMDocument没有__toString()方法,因此无法回显。

你可能正在寻找

echo $dom->saveHTML();

答案 4 :(得分:10)

有两个错误:第二个是因为$ dom不是字符串而是一个对象,因此无法“回显”。第一个错误是来自loadHTML的警告,这是由要加载的html文档的无效语法引起的(可能是&amp;用作参数分隔符,而不是用&amp;屏蔽为实体)。

通过使用错误控制操作符“@”(http://www.php.net/manual/en/language.operators.errorcontrol.php

调用该函数,忽略并抑制此错误消息(不是错误,只是消息!)
$dom->@loadHTML($html);

答案 5 :(得分:9)

无论echo(需要用print_r或var_dump替换),如果抛出异常,对象应保持为空:

DOMNodeList Object
(
)

<强>解决方案

  1. recover设为true,将strictErrorChecking设为false

    $content = file_get_contents($url);
    
    $doc = new DOMDocument();
    $doc->recover = true;
    $doc->strictErrorChecking = false;
    $doc->loadHTML($content);
    
  2. 在标记的内容上使用php的实体编码,这是最常见的错误来源。

答案 6 :(得分:8)

替换简单的

$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;
    }

答案 7 :(得分:6)

$html = file_get_contents("http://www.somesite.com/");

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

echo $dom;

试试这个

答案 8 :(得分:3)

另一种可能的解决方案是

$sContent = htmlspecialchars($sHTML);
$oDom = new DOMDocument();
$oDom->loadHTML($sContent);
echo html_entity_decode($oDom->saveHTML());

答案 9 :(得分:2)

我知道这是一个老问题,但如果您想修复格式错误的&#39;&amp;&#39;在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, '&amp;', $positionAmp, 1); // Replace straight away.
        } else if (preg_match('/&(#[0-9]+|[A-Z|a-z|0-9]+);/', $string) === 0) { // If a standard escape cannot be found.
            $html = substr_replace($html, '&amp;', $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.
        }
    }
}

答案 10 :(得分:0)

另一种可能的解决方案是,也许您的文件是ASCII类型的文件,只需更改文件的类型即可。

答案 11 :(得分:-1)

即使在此之后,我的代码也能正常工作,所以我只是在第1行删除了此语句的所有警告消息。

<?php error_reporting(E_ERROR); ?>