2014-10-28 56 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