如何使用dom php解析器

前端之家收集整理的这篇文章主要介绍了如何使用dom php解析器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚在 PHP中解析DOM:
我有一个我想要解析的HTML文件.它有一堆像这样的DIV:
<div id="interestingBox"> 
   <div id="interestingdetails" class="txtnormal">
        <div>Content1</div>
        <div>Content2</div>
   </div>
</div>

<div id="interestingBox"> 
......

我正在尝试使用PHP获取许多div框的内容.
如何使用DOM解析器来做到这一点?

谢谢!

解决方法

首先我要告诉你,你不能在两个不同的div上使用相同的id;那里有类.每个元素都应该有唯一的ID.

用id =“funBox获取div的内容代码

$html = '
<html>
<head></head>
<body>
<div id="interestingBox"> 
   <div id="interestingdetails" class="txtnormal">
        <div>Content1</div>
        <div>Content2</div>
   </div>
</div>

<div id="interestingBox2"><a href="#">a link</a></div>
</body>
</html>';


$dom_document = new DOMDocument();

$dom_document->loadHTML($html);

//use DOMXpath to navigate the html with the DOM
$dom_xpath = new DOMXpath($dom_document);

// if you want to get the div with id=interestingBox
$elements = $dom_xpath->query("*/div[@id='interestingBox']");

if (!is_null($elements)) {

  foreach ($elements as $element) {
    echo "\n[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }

  }
}

//OUTPUT
[div]  {
        Content1
        Content2
}

类别示例:

$html = '
<html>
<head></head>
<body>
<div class="interestingBox"> 
   <div id="interestingdetails" class="txtnormal">
        <div>Content1</div>
        <div>Content2</div>
   </div>
</div>

<div class="interestingBox"><a href="#">a link</a></div>
</body>
</html>';

//the same as before.. just change the xpath

[...]

$elements = $dom_xpath->query("*/div[@class='interestingBox']");

[...]

//OUTPUT
[div]  {
        Content1
        Content2
}

[div]  {
a link
}

有关详细信息,请参阅DOMXPath页.

原文链接:https://www.f2er.com/html/230370.html

猜你在找的HTML相关文章