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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退