php - 查找包含特定字符串的 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;
}
解决方案
试试这种方式,看看它是否有效:
编辑:
由于您DOMDocument
用于解析 XML,因此您不妨使用它的 xpath 支持来简洁地指定您要查找的内容:
$target = $xpath->query("//div[contains(.,'Trees')]");
而已。其余的只是一种输出筛选字符串表示形式的方法,以 XML 格式显示您所找到的内容:
$trees = $target[0]->ownerDocument->saveXML($target[0]);
echo $trees;
推荐阅读
- javascript - 使用 JavaScript 隐藏单选按钮的文本
- oracle - Jenkins 容器:Groovy 脚本因 jbdc 异常而失败
- javascript - 无法从模式中的建议列表中带来
- excel - 尝试分配单元格值,但不断提醒“类型不匹配”
- arrays - 使用函数复制数组
- javascript - 在 GeoServer 上发布 OpenLayers VectorTiles
- elasticsearch - 布尔查询中的不同分数函数
- chart.js - Chart.js 按月\年分组数据
- javascript - 如何计算多个航点之间的航点?
- c# - 以安全的方式在企业应用程序中运行 multipe Task<>