doukang5966907 2017-04-25 14:48
浏览 88
已采纳

PHP DOM getElementsbytagname()getElementById()

I'm working in PHP and looking at the DOM to parse html and find certain information on the page.

I have code working well using the getElementsbyTagName():

foreach ($dom->getElementsByTagName('div') as $node) {
          $array_data[ ] = $node->nodeValue;
        }

print_r($array_data);

On the page I'm looking at there are a lot of <div>'s. The div i'm interested in has a unique id on the page. I thought I could select this specific information By updating my code to look at the id as below:

foreach ($dom->getElementById('tree___34LHOENOP7') as $node) {
          $array_data[ ] = $node->nodeValue;
        }

print_r($array_data);

This code does not seem to work. The html:

<div id="tree___34LHOENOP7" style="margin-top: 5px; font-family: Verdana, Arial, sans-serif; height: 528px; overflow: auto;">
<ul><li style="list-style-type:none;"><span style="cursor:pointer;color:#D99F00;" onclick="prtv_C_E(this,'ExpandableRegionClickHandler','',2,true);(2,this);">UK</span><img src="https://images... " title="re-centre map here" onclick="ExpandableRegionClickHandler(2,this);" style="display:inline;margin-left:7px;cursor:pointer;"><ul style="display:block;"><li style="list-style-type:none;"><span style="cursor:pointer;color:#D99F00;" onclick="prtv_C_E(this,'ExpandableRegionClickHandler','',59,true);ExpandableRegionClickHandler(59,this);">Channel Islands</span><img src="https://images... " title="re-centre map here" onclick="ExpandableRegionClickHandler(59,this);" style="display:none;margin-left:7px;cursor:pointer;"><ul style="display:none;"><li style="list-style-image:url('http://images…’);"><a class="MapEventName" href="http://www... /guernsey" onclick="EventSpanClick(1439,this);" onmouseover="EventSpanMouseOver(1439,this);" onmouseout="EventSpanMouseOut(1439,this);">Guernsey</a></li>
  • 写回答

3条回答 默认 最新

  • dongpo1203 2017-04-25 15:33
    关注

    Hope this will help you out, DOMDocument::getElementById will return DOMElement Object.

    $domObject= new DOMDocument();
    $domObject->loadHTML($source);//put your complete source html string here
    $result=$domObject->getElementById("tree___34LHOENOP7");//this will output DOMElement Object
    print_r($result->nodeValue);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?