我构建了一个脚本,将页面上的所有 css 组合在一起,以便在我的 cms 中使用它。它工作了很长时间,现在我收到了这个错误:
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag header invalid in Entity, line: 10 in css.php on line 26
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag nav invalid in Entity, line: 10 in css.php on line 26
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Tag section invalid in Entity, line: 22 in css.php on line 26
This is the php script
这是我的代码:
<?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;
}
最佳答案
Header、Nav 和 Section 是来自 HTML5 的元素。因为 HTML5 开发者觉得记住 Public 和 System Identifiers 太难了,所以 DocType 声明就是:
<!DOCTYPE html>
换句话说,没有要检查的 DTD,这将使 DOM 使用 HTML4 过渡 DTD 并且不包含那些元素,因此警告。
要取消警告,请放置
libxml_use_internal_errors(true);
在调用 loadHTML
和
libxml_use_internal_errors(false);
在它之后。
另一种方法是使用 https://github.com/html5lib/html5lib-php .
关于php - DOMDocument::loadHTML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9149180/