-1
我想在PHP中使用DOMDocument类来解析HTML文件。
样本HTML文件如何在php DOMDocument类中通过ID获取元素?
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
</head>
<body>
<p id="myparagraph"></p>
</body>
</html>
,我使用
$document = new DOMDocument();
$document->loadHtmlFile("page.html");
现在我试图让通过ID p元素这样
print_r($document->getElementById("myparagraph"));
这不起作用装好了因为某些原因。那么这有什么问题?
你有DOCTYPE吗?如果不是,它将始终返回NULL。 – 2011-01-05 18:43:02
埃文工作!非常感谢:D,我添加了一个文档类型,现在它工作正常。 – Lettuce 2011-01-05 18:45:42