gpt4 book ai didi

php - 删除特定表 DOMXPath

转载 作者:行者123 更新时间:2023-12-02 04:02:26 25 4
gpt4 key购买 nike

我有这段代码并使用 DOMXPath 删除给定的 td

$html = file_get_contents('WebProxy.html');
$xml = new DOMDocument();
$xml->validateOnParse = true;
@$xml->loadHTML($html);

$xpath = new DOMXPath($xml);
$table =$xpath->query("//*[@id='proxylisttable']")->item(0);

// for printing the whole html table just type: print $xml->saveXML($table);

$rows = $table->getElementsByTagName("tr");

foreach ($rows as $row) {
$cells = $row -> getElementsByTagName('td');
foreach ($cells as $cell) {
echo $cell->nodeValue. . '<br>';
}
}

我会像这样做一样删除 td3、TD6、td7、td8

WebProxy.html

td1: <td class=" ">116.226.187.242</td>
td2: <td class=" ">1080</td>
td3: <td class=" ">CN</td>
td4: <td class=" ">China</td>
td5: <td class=" ">Socks4</td>
td6: <td class=" ">Anonymous</td>
td7: <td class=" ">Yes</td>
td8: <td class=" ">1 minute ago</td>
</tr>
<tr class="even">
td1: <td class=" ">23.254.153.205</td>
td2: <td class=" ">60088</td>
td3: <td class=" ">US</td>
td4: <td class=" ">United States</td>
td5: <td class=" ">Socks5</td>
td6: <td class=" ">Anonymous</td>
td7: <td class=" ">Yes</td>
td8: <td ctd1: lass=" ">1 minute ago</td>
</tr>
<tr class="odd">
td1: <td class=" ">46.101.208.9</td>
td2: <td class=" ">1080</td>
td3: <td class=" ">DE</td>
td4: <td class=" ">Germany</td>
td5: <td class=" ">Socks4</td>
td6: <td class=" ">Anonymous</td>
td7: <td class=" ">Yes</td>
td8: <td class=" ">1 minute ago</td>
</tr>

是这样的:

116.226.187.242
1080
China
Socks4

...

我该怎么办,谢谢你的帮助

最佳答案

也许您可以调整 xpath 查询,排除不需要的单元格。

例如:

<?php
$html = file_get_contents('WebProxy.html');
$xml = new DOMDocument();
$xml->validateOnParse = true;
@$xml->loadHTML($html);

$xpath = new DOMXPath($xml);
$cells =$xpath->query("//*[@id='proxylisttable']/tr/td[position() != 3 and position() != 6 and position() != 7 and position() != 8]");

foreach ($cells as $cell) {
echo $cell->nodeValue . "<br>";
}

关于php - 删除特定表 DOMXPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857894/

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