2014-01-01 09:55
浏览 33

使用php dom获取标题的文本

I have that HTML code

$html =  "<h1 id='h1>hhhh111</h1>"
        ."<h1 id='h2'>hhhh222</h1>"
        ."<h1 id='h3'>hhhh333</h1>";

I want to print "hhhh222" using DOM and PHP

I am trying that one -

$dom = new DOMDocument;
echo $dom->getElementsByTagName('h1')[1];

But not working so please help to print "hhhh222" using DOM and PHP.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongxu1875 2014-01-01 09:58

    Try this.

    echo $dom->getElementsByTagName('h1')->item(1)->nodeValue;
    点赞 打赏 评论
  • douju2014 2014-01-01 10:09

    You can also do like this:

    $html = "<h1 id='h1' >hhhh111</h1>
    <h1 id='h2' >hhhh222</h1>
    <h1 id='h3' >hhhh333</h1>";
    $dom = new DomDocument();
    $xpath = new DomXpath($dom);
    $div = $xpath->query("//*[@id='h2']")->item(0)->nodeValue;
          echo $div;
    点赞 打赏 评论

相关推荐 更多相似问题