PHP的HTML DomDocument getElementById问题 [英] PHP HTML DomDocument getElementById problems

查看:201
本文介绍了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){
}
NULL
pre>

我不应该查找< b> 标签,因为它的确有ID ?

解决方案

手册解释了为什么:


为了使这个功能起作用,需要使用DOMElement-> setIdAttribute()设置一些ID属性,或者将定义属性的DTD设置为ID类型。在后一种情况下,您需要在使用此函数前使用DOMDocument-> validate()或DOMDocument-> validateOnParse验证文档。




<通过一切手段,去有效的HTML&提供DTD。



快速修正:


  1. 调用 $ dom-> validate(); ,然后忍受错误(或解决它们),之后您可以使用 $ dom-> getElementById(),无论出于某种原因的错误。

  2. 如果您不想使用XPath,请使用XPath: $ x = new DOMXPath($ dom) ; $ el = $ x-> query(// * [@ id ='bid']) - > item(0);
  3. 想想看:如果你在加载HTML之前将 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:

For 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.

By all means, go for valid HTML & provide a DTD.

Quick fixes:

  1. 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.
  2. Use XPath if you don't feel like validing: $x = new DOMXPath($dom); $el = $x->query("//*[@id='bid']")->item(0);
  3. 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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆