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

gwe*_*weg 80 php

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

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

echo $dom;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Dew*_*rld 139

要消除警告,您可以使用 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);
Run Code Online (Sandbox Code Playgroud)


mat*_*ndr 89

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

<a href="/script.php?foo=bar&hello=world">link</a>
Run Code Online (Sandbox Code Playgroud)

应该

<a href="/script.php?foo=bar&amp;hello=world">link</a>
Run Code Online (Sandbox Code Playgroud)

  • ...并进一步扩展,在字符串上调用`htmlentities()`或类似将解决问题. (20认同)
  • 为了扩展这一点,如果&字符甚至是文本而不是HTML属性,它仍然需要转义为&amp ;. 解析器抛出错误的原因是因为看到一个&它正在期待一个; 终止HTML实体. (3认同)

Maa*_*oyy 52

$dom->@loadHTML($html);
Run Code Online (Sandbox Code Playgroud)

这是不正确的,请改用:

@$dom->loadHTML($html);
Run Code Online (Sandbox Code Playgroud)

  • 或$ dom-> strictErrorChecking = false; (25认同)
  • 这是一个糟糕的解决方案,因为你会在这一行上犯错误进行调试.@ Dewsworld的解决方案要好得多. (5认同)
  • 这是一个非常脏的解决方案,这不会解决所有问题. (2认同)
  • 虽然您的答案可以解决该问题,但“这是不正确的”这一行本身就是不正确的。 (2认同)

Mik*_*e B 12

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

你可能正在寻找

echo $dom->saveHTML();
Run Code Online (Sandbox Code Playgroud)


小智 10

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

您通过使用错误控制运算符"@"调用该函数来忽略并抑制此错误消息(不是错误,只是消息!)(http://www.php.net/manual/en/language.operators.errorcontrol. php)

$dom->@loadHTML($html);
Run Code Online (Sandbox Code Playgroud)


Lor*_*uer 10

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

DOMNodeList Object
(
)
Run Code Online (Sandbox Code Playgroud)

  1. 设置recover为true,strictErrorChecking为false

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


Dav*_*han 8

取代简单

$dom->loadHTML($html);
Run Code Online (Sandbox Code Playgroud)

更健壮......

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;
    }
Run Code Online (Sandbox Code Playgroud)


Nic*_*tte 6

我知道这是一个老问题,但是如果您想修复 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 escape the '&' sign.
            fixAmps($html, $positionAmp+5); // Recursive call from the new position.
        } else {
            fixAmps($html, $positionAmp+1); // Recursive call from the new position.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

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

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

echo $dom;
Run Code Online (Sandbox Code Playgroud)

尝试这个