PHP DOM XPath获取HTML节点方法大全

PHP的有些技巧可能大家并不常用到,比如DOM相关的对象。

这些方法几乎和Javascript一样的方便,轻松一句就能获取到HTML DOM节点的数据。

相比于使用正则表达式,这个方法更简单快捷。

我就就常用DOMDocumentXPath两个类做一个介绍。

假设有这样一个HTML页面(部分),其内容如下:

$html = <<<HTML
    <div class="container">
        <img class="logo img" id="img1" src="/images/img1.jpg" />
        <img class="icon img" id="img2" src="/images/img2.jpg" />
        <img class="icon use" id="img3" src="/images/img3.jpg" />
        <p class="icon" id="content">Welcome PHP!</p>
    </div>
HTML;

我们把它赋值给字符串变量$html

我们将$html加载到DOM对象,再用DOMXPath解析处理。

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

1 DOMXPath用法

接下来我们将用DOMXPath的方法来解析。

DOMXPath有两个核心的部分:传入的表达式返回值

表达式是W3C标准的XPath表达式,语法:https://www.w3schools.com/xml/xpath_syntax.asp

返回值是DOMNodeList对象,有若干属性和方法,也是W3C标准:https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html

2 获取img src

获取第一个图片的src内容:

echo $src = $xpath->evaluate('string(//img/@src)');
/*输出:
/images/img1.jpg
*/

获取全部IMG SRC内容

$nodeList = $xpath->query("//img");
$srcList = [];
foreach ($nodeList as $node) {
    $srcList[] = $node->attributes->getNamedItem('src')->nodeValue;
}
print_r($srcList);

/*输出:
Array
(
 [0] => /images/img1.jpg
 [1] => /images/img2.jpg
 [2] => /images/img3.jpg
)
*/

3 获取特定class DOM

获取所有class等于contentid值,这里class值必须是唯一的:

$nodeList = $xpath->query('//*[@class="icon"]');
$result = [];
foreach ($nodeList as $node) {
    $result[] = $node->attributes->getNamedItem('id')->nodeValue;
}
print_r($result);
/*输出:
Array
(
    [0] => content
)
*/

获取所有class包含icon的节点的id值:

$nodeList = $xpath->query('//*[contains(@class, "icon")]');
$result = [];
foreach ($nodeList as $node) {
    $result[] = $node->attributes->getNamedItem('id')->nodeValue;
}
print_r($result);
/*输出:
Array
(
    [0] => img2
    [1] => img3
    [2] => content
)
*/

获取所有class包含icon的节点的完整HTML内容

$nodeList = $xpath->query('//*[contains(@class, "icon")]');
$result = [];
foreach ($nodeList as $node) {
    $result[] = $dom->saveHTML($node);
}
print_r($result);
/*输出:
Array
(
    [0] => <img class="icon img" id="img2" src="/images/img2.jpg">
    [1] => <img class="icon use" id="img3" src="/images/img3.jpg">
    [2] => <p class="icon" id="content">Welcome PHP!</p>
)
*/

 

参考地址:

  1. Get img src with PHP
  2. PHP DomXPath – Get Child by Class
  3. PHP DOM Xpath – search for class name
  4. Document Object Model (DOM) Level 3 Core Specification

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

昵称 *