php - DOMDocument::loadHTML 错误

标签 php html domdocument

我构建了一个脚本,将页面上的所有 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/

相关文章:

php - PHP DOM 有两个 HTML 问题:1.) DOMDocument::createEntityReference $name 值和 2.) 如何向一个元素添加多个 CSS 类?

php - 从 View 到模型都是可变的。多维 Controller

php - Mysql查询无重复!

javascript - 修改函数以替换文本以及按下按钮时添加克拉

javafx - 获取 JavaFX WebEngine 解释文档

php - 如何按类获取图片src

php - 重定向 301 不适用于 url 中的 php 变量

php - SimpleXML:选择具有特定属性值的元素

javascript - 显示内嵌文本的框 div

javascript - CSS/HTML - 使固定 div 占据屏幕的 20%,另一个可滚动