0

我正在尝试从 html 获取子节点的值。

我尝试了什么: -

$data = $dom->getElementById($identifier);
$node = $data->childNodes;
 foreach($node as $node)
    {
           echo $node->nodeName;
           echo $node->nodeValue;

            }
     }

我能够获取所有子节点值,即使子标签没有值我得到一个空字符串。

如果标签具有适当的值,有什么方法可以获取值???

更新:-

我的html数据:-

 <div id="myid"> //I give this id as input
 <h1> Some data 1</h1>
 <script type=".."> google adsense details </script>
 <p class="some class"> </p>
 <div class="some class1"></div>
 <h2>data2</h2>
 <p>SOme more data...blah blah..</p>
 </div>

我想要什么输出:-

  Some data 1
  data2
  SOme more data...blah blah..

我得到了什么:-

  Some data 1
  googleadsense details//i am getting values inside script as well
  //blanc data which includes many spaces of tag p 
  //blanc data which includes many spaces of tag div
  data2
  SOme more data...blah blah..
4

2 回答 2

1

经过一些测试,这应该适用于您要完成的工作:

第一次编辑:这个解决方案考虑了多个子节点循环通过内部标识符。

第二次编辑:此解决方案用于指定您不希望返回的标签/值。

第三次编辑:删除了原始问题的详细信息,与更新的问题无关。

 $dom = new DOMDocument();
 $html = '<div id="myid"> //I give this id as input<h1> Some data 1</h1><script type=".."> google adsense details </script><p class="some class"></p><div class="some class1"></div><h2>data2</h2><p>SOme more data...blah blah..</p></div>';
 $dom->loadHTML( $html ); 

 $identifier = "myid";
 $id_nodes = $dom->getElementById( $identifier );

 foreach( $id_nodes->childNodes as $node ) 
 {
    // Blacklist for what you do not want in your output:
    if( $node->nodeName != "script" && $node->nodeName != "#text" && $node->nodeValue != '' ) {
        echo $node->nodeValue . "<br />";
    }
 }

上述脚本的输出是:

Some data 1
data2
SOme more data...blah blah..
于 2013-03-13T06:23:24.867 回答
0

在回显之前检查值?

foreach($data as $node)
{
  if(strlen($node->nodeValue) > 0)
{
  echo $node->nodeName;
  echo $node->nodeValue;
}
}
于 2013-03-13T05:51:37.387 回答