简体   繁体   English

获取HTML中子节点的值

[英]getting the value of child nodes in html

I am trying to get the value of child nodes from html. 我试图从html获取子节点的值。

What i tried:- 我试过的:-

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

            }
     }

I am able to get all the childnodes values, even if the childtag has no value i am getting an empty string. 我可以获取所有childnodes的值,即使childtag没有任何值,我也得到了一个空字符串。

Is there any way to get values if the tag has a proper value ??? 如果标签具有适当的值,有什么方法可以获取值?

Updated:- 更新:-

My htmldata:- 我的htmldata:-

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

What output i want:- 我想要什么输出:-

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

WHat i am getting:- 我正在得到什么:-

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

After some testing, this should work for what you're trying to accomplish: 经过一些测试,这应该可以满足您要完成的任务:

1st EDIT: This solution accounts for multiple sub nodes to loop through inside identifier. 1st EDIT:此解决方案考虑到多个子节点循环通过内部标识符。

2nd EDIT: This solution accounts for specifying what tags/values you don't want returned. 第二编辑:此解决方案用于指定您不希望返回的标签/值。

3rd EDIT: Took out details intended for original question and really irrelevant to updated question. 第3编辑:拿出了原问题的详细内容,而这些内容与更新的问题完全无关。

 $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 />";
    }
 }

The output of the above script is: 上面脚本的输出是:

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

Check the value before echo? 在回显之前检查值?

foreach($data as $node)
{
  if(strlen($node->nodeValue) > 0)
{
  echo $node->nodeName;
  echo $node->nodeValue;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM