php DOMDocument::loadHTML 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9149180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DOMDocument::loadHTML error
提问by user1079160
I build a script that combines all css on a page together to use it in my cms. It worked fine for a long time now i i get this error:
我构建了一个脚本,将页面上的所有 css 组合在一起以在我的 cms 中使用它。它工作了很长时间,现在我收到此错误:
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag header invalid in Entity, line: 10 in css.phpon line 26
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag nav invalid in Entity, line: 10 in css.phpon line 26
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag section invalid in Entity, line: 22 in css.phpon line 26
This is the php script
警告:DOMDocument::loadHTML() [domdocument.loadhtml]:实体中的标记标头无效,第26行css.php中的第10 行警告:DOMDocument::loadHTML() [domdocument.loadhtml]:实体中的标记导航无效, line: 10 in css.phpon line 26 Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag section invalid in Entity, line: 22 in css.phpon line 26这是php脚本
This is my code:
这是我的代码:
<?php
header('Content-type: text/css');
include ('../global.php');
if ($usetpl == '1') {
$client = New client();
$tplname = $client->template();
$location = "../templates/$tplname/header.php";
$page = file_get_contents($location);
} else {
$page = file_get_contents('../index.php');
}
class StyleSheets extends DOMDocument implements IteratorAggregate
{
public function __construct ($source)
{
parent::__construct();
$this->loadHTML($source);
}
public function getIterator ()
{
static $array;
if (NULL === $array) {
$xp = new DOMXPath($this);
$expression = '//head/link[@rel="stylesheet"]/@href';
$array = array();
foreach ($xp->query($expression) as $node)
$array[] = $node->nodeValue;
}
return new ArrayIterator($array);
}
}
foreach (new StyleSheets($page) as $index => $file) {
$css = file_get_contents($file);
echo $css;
}
回答by Gordon
Header, Nav and Section are elements from HTML5. Because HTML5 developers felt it is too difficult to remember Public and System Identifiers, the DocType declaration is just:
Header、Nav 和 Section 是来自 HTML5 的元素。因为 HTML5 开发人员觉得公共标识符和系统标识符太难记住,所以 DocType 声明只是:
<!DOCTYPE html>
In other words, there is no DTD to check, which will make DOM use the HTML4 Transitional DTD and that doesnt contain those elements, hence the Warnings.
换句话说,没有要检查的 DTD,这将使 DOM 使用 HTML4 Transitional DTD 并且不包含这些元素,因此警告。
To surpress the Warnings, put
为了抑制警告,把
libxml_use_internal_errors(true);
before the call to loadHTML
and
在调用loadHTML
和之前
libxml_use_internal_errors(false);
after it.
之后。
An alternative would be to use https://github.com/html5lib/html5lib-php.
回答by dogatonic
With a DOMDocument object, you should be able to place an @ before the load method in order to SUPPRESS all WARNINGS.
对于 DOMDocument 对象,您应该能够在 load 方法之前放置一个 @ 以抑制所有警告。
$dom = new DOMDocument;
@$dom->loadHTML($source);
And carry on.
并继续。