2014-10-28 61 views
1

我認爲這會比過去更容易,或者我正在考慮這一點。PHP DomDocument Return Doctype

我想從網址返回HTML頁面聲明的DOCTYPE。 如果它不是!DOCTYPE html將其附加爲!DOCTYPE html。

我已經嘗試了幾個不同的東西......最新的是數組返回下面,但無論測試頁返回的數組是完全一樣的,即使完全聲明的DOCTYPE是不一樣的。

任何想法,我在做什麼錯或如何解決這將不勝感激。

<!DOCTYPE html> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 


$url="http://www.example.com"; 

$html = file_get_contents($url); 

$doc = new DOMDocument(); 
$doc->loadHTML(htmlentities($html)); 

$doctype = $doc->doctype; 
var_dump($doctype); 

的var_dump返回

object(DOMDocumentType)#2 (22) { 
["name"]=> string(4) "html" 
["entities"]=> string(22) "(object value omitted)" 
["notations"]=> string(22) "(object value omitted)" 
["publicId"]=> string(37) "-//W3C//DTD HTML 4.0 Transitional//EN" 
["systemId"]=> string(41) "http://www.w3.org/TR/REC-html40/loose.dtd" 
["internalSubset"]=> NULL 
["nodeName"]=> string(4) "html" 
["nodeValue"]=> NULL 
["nodeType"]=> int(10) 
["parentNode"]=> string(22) "(object value omitted)" 
["childNodes"]=> NULL 
["firstChild"]=> NULL 
["lastChild"]=> NULL 
["previousSibling"]=> NULL 
["nextSibling"]=> string(22) "(object value omitted)" 
["attributes"]=> NULL 
["ownerDocument"]=> string(22) "(object value omitted)" 
["namespaceURI"]=> NULL 
["prefix"]=> string(0) "" 
["localName"]=> NULL 
["baseURI"]=> NULL 
["textContent"]=> string(0) "" } 

回答

2

你不需要調用ヶ輛 $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html></html>'; $doc = new DOMDocument(); $doc->loadHTML($html); $doctype = $doc->doctype; var_dump($doctype);

+0

謝謝你....回首走過的10周其他的事情我想... 。我有你的確切的代碼,除了var_dump ...我試圖回聲它。 – 2014-10-28 22:34:05

+0

不客氣! – 2014-10-28 22:37:02