gpt4 book ai didi

php - Xpath选择多个标签

转载 作者:可可西里 更新时间:2023-10-31 22:12:48 24 4
gpt4 key购买 nike

我想要使用 PHP DOMXPath 查询的多个标签(td 和 th)。

我该怎么做?

最佳答案

您可以使用 |(联合)运算符。这是一个例子:

$doc = new DOMDocument();
$doc->loadHTML('<table>
<tr>
<th>table header</th>
<td>table cell</td>
</tr>
</table>');

$xpath = new DOMXPath($doc);

$rows = $xpath->query('//tr'); // select all <tr> elements anywhere in the document
$cols = $xpath->query('./th | ./td', $rows->item(0)); // select all <th>/<td> from context
// where context = first row
echo $cols->length; // 2
echo $cols->item(0)->nodeValue; // table header
echo $cols->item(1)->nodeValue; // table cell

关于php - Xpath选择多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com