use*_*160 51 php html5 domdocument
我构建了一个脚本,它将页面上的所有css组合在一起,以便在我的cms中使用它.它工作了很长时间,现在我得到这个错误:
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:实体中的标签头无效,第26行的css.php中的第10 行警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:实体中的标签导航无效,第26行的css.php中的第10 行警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:实体中的标记部分无效,第26行的css.php中的第 22行:这是php脚本
这是我的代码:
<?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;
}
Run Code Online (Sandbox Code Playgroud)
Gor*_*don 131
标题,导航和部分是HTML5中的元素.因为HTML5开发人员觉得记住公共标识符和系统标识符太难了,所以DocType声明只是:
<!DOCTYPE html>
Run Code Online (Sandbox Code Playgroud)
换句话说,没有要检查的DTD,这将使DOM使用HTML4 Transitional DTD并且不包含这些元素,因此警告.
为了压制警告,放
libxml_use_internal_errors(true);
Run Code Online (Sandbox Code Playgroud)
在致电loadHTML
和之前
libxml_use_internal_errors(false);
Run Code Online (Sandbox Code Playgroud)
在它之后.
另一种方法是使用https://github.com/html5lib/html5lib-php.
Dha*_*man 15
仍然不支持 HTML5 元素,但您可以使用该$options
参数完全消除 libxml 错误。
刚刚设置
$doc = new DOMDocument();
$doc->loadHTMLFile("html5.html", LIBXML_NOERROR);
Run Code Online (Sandbox Code Playgroud)
该选项优于@
消除 PHP 错误的选项。
但要小心,libxml 非常宽容,它会解析损坏的 HTML 文档。如果您静默 libxml 错误,您甚至可能不会意识到 HTML 格式错误。
dog*_*nic 11
使用DOMDocument对象,应该可以在加载方法之前放置@,以便禁止所有警告。
$dom = new DOMDocument;
@$dom->loadHTML($source);
Run Code Online (Sandbox Code Playgroud)
并继续。