我有一个 HTML 表格,其结构如下:
<tr>
<td class='tablesortcolumn'>atest</td>
<td >Kunde</td>
<td ><a href=""><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f6fef2faffd3f6ebf2fee3fff6bdf0fcfe" rel="noreferrer noopener nofollow">[email protected]</a></a></td>
<td align="right"><a href="module/dns_reseller/user_edit.php?ns=3&uid=6952"><img src="images/iconedit.gif" border="0"/></a> <img src="images/pixel.gif" width="2" height="1" border="0"/> <a href="module/dns_reseller/user.php?delete=true&uid=6952" onclick="return confirm('Möchten Sie den Datensatz wirklich löschen?');"><img src="images/icontrash.gif" border="0"/></a></td>
</tr>
有数百个 tr
block 。
我要提取atest
和<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2878f838b8ea2879a838f928e87cc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>
我尝试了以下方法:
$document = new DOMDocument();
$document->loadHTML($data);
$selector = new DOMXPath($document);
$elements = $selector->query("//*[contains(@class, 'tablesortcolumn')]");
foreach($elements as $element) {
$text = $element->nodeValue;
print($text);
print('<br>');
}
提取atest没有问题,因为我可以获取带有tablesortcolumn
的元素类(class)。我如何获得电子邮件地址?
我不能简单地使用//table/tr/td/a
因为网站上还有其他类似结构的元素。所以我需要通过选择一个空的 href 标签来获取它。我已经尝试过//table/tr/td/a[contains(@href, '')]
但它返回的结果与 //table/tr/td/a
相同
有人知道如何解决这个问题吗?
最佳答案
您可以尝试运行包含字符串@
的xpath吗?这似乎不太可能用于其他用途。
所以像这样的东西可能会起作用
//*[text()[contains(.,'@')]]
关于php - 提取html代码中的两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630883/