请原谅我的英语。
我想读一下第一个"tr“中的锚名和href。我得到的是什么但不是名字..。
有人能帮我吗?
$dom = new domDocument;
@$dom->loadHTML($content2);
$dom->preserveWhiteSpace = true;
$xpath = new DOMXPath($dom);
$rows = $xpath->query('//tr');
foreach ($rows as $row) {
$cols = $row->getElementsByTagName('td');
foreach ($cols as $col) {
$test = $col->nodeValue;
if ($xpath->evaluate('count(./a)', $col) > 0) { // check if an anchor exists
$link = $xpath->evaluate('string(./a/@href)', $col); // if there is, then echo the href value
}
}
}
<table width="100%" border="0">
<tr>
<td style="width:20px;"><img src="images/gfx/programm.png" style="cursor: pointer;"> </td>
<td style="width:415px;"><a href="?id=492488" onmouseover="Kurzinfo(492488)" onmouseout="hideit(492488)">Wondershare Filmora - 8.2.2.1 Patch</a> </td>
<td nowrap="nowrap" style="text-align:right;">Appz </td>
<td style="width:40px;text-align:right;">220 </td>
<td style="width:20px;"> MB </td>
<td style="width:150px;text-align:right;" nowrap="nowrap">21.05.2017 10:23:48 Uhr </td>
</tr>
<tr>发布于 2017-05-21 13:04:02
我猜想您想要获得Wondershare Filmora - 8.2.2.1 Patch,因为您可以使用text(),即:
$dom = new domDocument;
@$dom->loadHTML($content2);
$dom->preserveWhiteSpace = true;
$xpath = new DOMXPath($dom);
$rows = $xpath->query('//tr');
foreach ($rows as $row) {
$cols = $row->getElementsByTagName('td');
foreach ($cols as $col) {
$test = $col->nodeValue;
if ($xpath->evaluate('count(./a)', $col) > 0) { // check if an anchor exists
$link = $xpath->evaluate('string(./a/@href)', $col); // get the href value
$text = $xpath->evaluate('string(./a/text())', $col); // get the href text value
}
}
}发布于 2017-05-21 13:56:29
我想读一下第一个"tr“中的锚名和href。我得到的是什么,但不是名字
第一个tr可以表示为//table//tr[1]。类似地,可以使用tr表达式获取第一个(//table//tr[1]/td//a)[1]的第一个锚点,例如:
$anchor_list = $xpath->query('(//table//tr[1]/td//a)[1]');其中$anchor_list要么是DOMNodeList的实例,要么是FALSE。元素(DOMElement的实例)可以使用[]操作符访问。
if ($anchor_list && $anchor_list->length) {
$a = $anchor_list[0];
}有了DOMElement,我们就可以通过attributes属性轻松地访问它的属性:
$href = $a->attributes->getNamedItem('href');
var_dump($href ? $href->textContent : "(none)");
var_dump($name ? $name->textContent : "(none)");如果要获取锚节点的内部文本,请使用textContent属性:
var_dump($a->textContent);https://stackoverflow.com/questions/44096979
复制相似问题