本文介绍了PHP的HTML DomDocument getElementById问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里有一点新的PHP解析,但我似乎无法让PHP的DomDocument返回显然是可识别节点的东西。加载的HTML将来自网络,因此不一定能保证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;
/ ***将html加载到对象中*** /
$ dom-> loadHTML($ html);
var_dump($ dom);
$ belement = $ dom-> getElementById(bid);
var_dump($ belement);
?>
尽管我没有收到任何错误,但我只收到以下内容作为输出:
object(DOMDocument)#1(0){
pre>
}
NULL
我不应该查找
< b>
标签,因为它的确有ID ?解决方案解释了为什么:
快速修正:
- 调用
$ dom-> validate();
,然后忍受错误(或解决它们),之后您可以使用$ dom-> getElementById()
,无论出于某种原因的错误。
- 如果您不想使用XPath,请使用XPath:
想想看:如果你在加载HTML之前将$ x = new DOMXPath($ dom) ; $ el = $ x-> query(// * [@ id ='bid']) - > item(0);
validateOnParse
设置为true ,如果也可以; P
。
$ dom = new DOMDocument();
$ html ='< html>
< body>您好< b id =bid>全球< / b>。< / body>
< / html>';
$ dom-> validateOnParse = true; //<! - 第一个
$ dom-> loadHTML($ html); //'cause'load'=='parse
$ dom-> preserveWhiteSpace = false;
$ belement = $ dom-> getElementById(bid);
echo $ belement-> nodeValue;
在此处输出'World'。
A little new to PHP parsing here, but I can't seem to get PHP's DomDocument to return what is clearly an identifiable node. The HTML loaded will come from the 'net so can't necessarily guarantee XML compliance, but I try the following:
<?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); ?>
Though I receive no error, I only receive the following as output:
object(DOMDocument)#1 (0) { } NULL
Should I not be able to look up the
<b>
tag as it does indeed have an id?解决方案The Manual explains why:
By all means, go for valid HTML & provide a DTD.
Quick fixes:
- Call
$dom->validate();
and put up with the errors (or fix them), afterwards you can use$dom->getElementById()
, regardless of the errors for some reason.- Use XPath if you don't feel like validing:
$x = new DOMXPath($dom); $el = $x->query("//*[@id='bid']")->item(0);
- Come to think of it: if you just set
validateOnParse
to true before loading the HTML, if would also work ;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;
Outputs 'World' here.
这篇关于PHP的HTML DomDocument getElementById问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!