Php domdocument xpath获取第一个tr锚点名称和href(不带id或类)

Php domdocument xpath获取第一个tr锚点名称和href(不带id或类),php,dom,xpath,Php,Dom,Xpath,请原谅我的英语 我想从第一个“tr”中读取锚点名称和href。我得到的href,但不是名称 有人能帮我吗 $dom = new domDocument; @$dom->loadHTML($content2); $dom->preserveWhiteSpace = true; $xpath = new DOMXPath($dom); $rows = $xpath->query('//tr'); f

请原谅我的英语

我想从第一个“tr”中读取锚点名称和href。我得到的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>
$dom=新的domDocument;
@$dom->loadHTML($content2);
$dom->preserveWhiteSpace=true;
$xpath=newdomxpath($dom);
$rows=$xpath->query('//tr');
foreach($行作为$行){
$cols=$row->getElementsByTagName('td');
foreach($cols作为$col){
$test=$col->nodeValue;
如果($xpath->evaluate($count(./a),$col)>0){//检查是否存在锚定
$link=$xpath->evaluate($string(./a/@href)$col);//如果有,则回显href值
}
}
}
阿普斯
220
兆字节
2017年5月21日10:23:48 Uhr

我猜您希望获得
Wondershare Filmora-8.2.2.1补丁
,因此您可以使用
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
        }
    }
}


我猜您希望获得
Wondershare Filmora-8.2.2.1补丁
,因此您可以使用
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
        }
    }
}

我想从第一个“tr”中读取锚点名称和href。我得到的href,但不是名称

第一个
tr
可以表示为
//表//tr[1]
。类似地,可以使用
(//table//tr[1]/td//a)[1]
表达式获取第一个
tr
的第一个锚点,例如:

$anchor_list = $xpath->query('(//table//tr[1]/td//a)[1]');
其中,
$anchor\u list
DOMNodeList
的实例,或
FALSE
。可以使用
[]
运算符访问元素(DOMELENT的实例)

if ($anchor_list && $anchor_list->length) {
  $a = $anchor_list[0];
}
拥有
doElement
我们可以通过
attributes
属性轻松访问其属性:

$href = $a->attributes->getNamedItem('href');

var_dump($href ? $href->textContent : "(none)");
var_dump($name ? $name->textContent : "(none)");
var_dump($a->textContent);
如果要获取锚节点的内部文本,请使用
textContent
属性:

$href = $a->attributes->getNamedItem('href');

var_dump($href ? $href->textContent : "(none)");
var_dump($name ? $name->textContent : "(none)");
var_dump($a->textContent);
我想从第一个“tr”中读取锚点名称和href。我得到的href,但不是名称

第一个
tr
可以表示为
//表//tr[1]
。类似地,可以使用
(//table//tr[1]/td//a)[1]
表达式获取第一个
tr
的第一个锚点,例如:

$anchor_list = $xpath->query('(//table//tr[1]/td//a)[1]');
其中,
$anchor\u list
DOMNodeList
的实例,或
FALSE
。可以使用
[]
运算符访问元素(DOMELENT的实例)

if ($anchor_list && $anchor_list->length) {
  $a = $anchor_list[0];
}
拥有
doElement
我们可以通过
attributes
属性轻松访问其属性:

$href = $a->attributes->getNamedItem('href');

var_dump($href ? $href->textContent : "(none)");
var_dump($name ? $name->textContent : "(none)");
var_dump($a->textContent);
如果要获取锚节点的内部文本,请使用
textContent
属性:

$href = $a->attributes->getNamedItem('href');

var_dump($href ? $href->textContent : "(none)");
var_dump($name ? $name->textContent : "(none)");
var_dump($a->textContent);

你在说什么?你在说什么?