首页 > 解决方案 > 查找包含特定字符串的 Div 的子元素

问题描述

我正在尝试查找包含特定字符串的 div 的所有子元素。例如,在下面的 HTML 内容中,我需要找到 "Trees" div 的所有子元素,包括<div>Trees对。每个 div 都没有关联的类或 ID,因此我无法搜索 ID 或类。

我使用来自https://stackoverflow.com/a/55989111/1466973的答案尝试了以下代码,但该函数未返回预期的内容。

<?php
$html_text = "
<html>
<div>Grass
    <div>Good grass
        <div>Grass 1</div>
        <div>Grass 2</div>
        <div>Grass 3</div>
    </div>
    <div>Weeds
        <div>Weeds 2</div>
        <div>Weeds 3</div>
        <div>Weeds 4</div>
    </div>
</div>

<div>Trees
    <div>Good Trees
        <div>Tree 1</div>
        <div>Tree 2</div>
        <div>Tree 3</div>
    </div>
    <div>Tall Trees
        <div>Tree 11</div>
        <div>Tree 12</div>
        <div>Tree 13</div>
    </div>
</div>

<div>Fruit
    <div>Red
        <div>Fruit 1</div>
        <div>Fruit 2</div>
        <div>Fruit 31</div>
    </div>
</div>
</html> ";

echo find_content($html_text);  // this should be only the content of the div containing "Trees"

// tried this solution from https://stackoverflow.com/a/55989111/1466973 , didn't work
function find_trees($html_text = "") {
    $dom = new DOMDocument();
    $dom->loadHTML($html_text);
    $xpath = new DOMXpath($dom);

    $res = $xpath->document->documentElement->textContent;

    $textNodes = explode(PHP_EOL, $res);
    $trees_html = "";
    foreach ($textNodes as $key => $text) {
        if ($text == 'Trees') {
            $trees_html .= $textNodes[$key + 1];
            break;
        }
    }
    "end of this function<br>";
 return $trees_html;
 }

标签: phpdom

解决方案


试试这种方式,看看它是否有效:

编辑:

由于您DOMDocument用于解析 XML,因此您不妨使用它的 xpath 支持来简洁地指定您要查找的内容:

 $target = $xpath->query("//div[contains(.,'Trees')]");

而已。其余的只是一种输出筛选字符串表示形式的方法,以 XML 格式显示您所找到的内容:

 $trees = $target[0]->ownerDocument->saveXML($target[0]);
    echo $trees;

推荐阅读