2012-02-05 106 views
41

我構建了一個腳本,它將頁面上的所有css結合在一起以便在我的cms中使用它。它能正常工作了很長時間,現在我我得到這個錯誤:
DOMDocument :: loadHTML error


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; 
} 
+1

此問題已在https://bugs.php.net/bug.php?id=60021上針對PHP進行了報告,而這又在底層libxml2中產生了功能請求:https://bugzilla.gnome.org/ show_bug.cgi?id = 761534 – cweiske 2016-02-04 07:58:26

回答

109

頁眉,導航和區由HTML5元素。由於HTML5開發人員覺得實在是太難記住公共和系統標識符,DOCTYPE聲明僅僅是:

<!DOCTYPE html> 

換句話說,沒有DTD檢查,這將使得DOM使用HTML4過渡DTD和不包含這些元素,因此警告。

要surpress的警告,把

libxml_use_internal_errors(true); 

該呼叫之前loadHTML

libxml_use_internal_errors(false); 

後。

另一種方法是使用https://github.com/html5lib/html5lib-php

+2

這樣做,現在我得到一個空白頁 – user1079160 2012-02-05 12:40:19

+2

@ user1079160這是另一個問題!戈登有很好的答案,謝謝! – 2012-11-20 13:00:49

+1

@Gordan你如何解決空白頁問題? – CodeGuru 2015-02-16 23:14:42

相關問題