这里的
PHP解析有点新,但我似乎无法让
PHP的DomDocument返回显然是可识别的节点.加载的HTML将来自’net,因此无法保证XML合规性,但我尝试以下方法:
<?PHP header("Content-Type: text/plain"); $html = '<html><body>Hello <b id="bid">World</b>.</body></html>'; $dom = new DomDocument; $dom->preserveWhiteSpace = false; $dom->validateOnParse = true; /*** load the html into the object ***/ $dom->loadHTML($html); var_dump($dom); $belement = $dom->getElementById("bid"); var_dump($belement); ?>
object(DOMDocument)#1 (0) { } NULL
我是否应该无法查找< b>标签,因为它确实有一个ID?
The Manual解释了原因:
原文链接:https://www.f2er.com/php/134848.htmlFor this function to work,you will need either to set some ID attributes with DOMElement->setIdAttribute() or a DTD which defines an attribute to be of type ID. In the later case,you will need to validate your document with DOMDocument->validate() or DOMDocument->validateOnParse before using this function.
无论如何,请使用有效的HTML&提供DTD.
快速修复:
>拨打$dom-> validate();并忍受错误(或修复它们),之后您可以使用$dom-> getElementById(),无论出于某种原因的错误.
>如果你不喜欢validing,请使用XPath:$x = new DOMXPath($dom); $el = $x-> query(“// * [@ id =’bid’]”) – > item(0);
>想一想:如果你只是在加载HTML之前将validateOnParse设置为true,那么它也会起作用; P
.
$dom = new DOMDocument(); $html ='<html> <body>Hello <b id="bid">World</b>.</body> </html>'; $dom->validateOnParse = true; //<!-- this first $dom->loadHTML($html); //'cause 'load' == 'parse $dom->preserveWhiteSpace = false; $belement = $dom->getElementById("bid"); echo $belement->nodeValue;
在这里输出’世界’.