php DOMXpath - 获取 a 元素的 href 属性和文本值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6820429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:22:17  来源:igfitidea点击:

DOMXpath - Get href attribute and text value of an a element

phpxpathdomxpath

提问by Marko Jovanovi?

So I have a HTML string like this:

所以我有一个像这样的 HTML 字符串:

<td class="name">
   <a href="/blah/somename23123">Some Name</a>
</td>
<td class="name">
   <a href="/blah/somename28787">Some Name2</a>
</td>

Using XPath I'm able to get value of href attribute using this Xpath query:

使用 XPath,我可以使用此 Xpath 查询获取 href 属性的值:

 $domXpath = new \DOMXPath($this->domPage);
 $hrefs = $domXpath->query("//td[@class='name']/a/@href");
 foreach($hrefs as $href) {...}

And It's even easier to get a text value, like this:

获取文本值更容易,如下所示:

 // Xpath auto. strips any html tags so we are 
 // left with clean text value of a element
 $domXpath = new \DOMXPath($this->domPage);
 $names = $domXpath->query("//td[@class='name']/");
 foreach($names as $name) {...}

Now I'm curious to know, how can I combine those two queries to get both values with only one query (If it's something like that even posible?).

现在我很想知道,我怎样才能将这两个查询结合起来只用一个查询来获得两个值(如果它甚至可能是这样的?)。

回答by Gordon

Fetch

拿来

//td[@class='name']/a

and then pluck the text with nodeValueand the attribute with getAttribute('href').

然后用 提取文本nodeValue和属性getAttribute('href')

Apart from that, you can combine Xpath queries with the Union Operator |so you can use

除此之外,您可以将 Xpath 查询与联合运算符结合使用,|以便您可以使用

//td[@class='name']/a/@href|//td[@class='name']

as well.

以及。

回答by Ryan

To reduce the code to a single loop, try:

要将代码减少到单个循环,请尝试:

$anchors = $domXpath->query("//td[@class='name']/a");
foreach($anchors as $a)
{ 
    print $a->nodeValue." - ".$a->getAttribute("href")."<br/>";
}

As per above :) Too slow ..

如上所述:) 太慢了..

回答by Peter Krauss

Simplest way, evaluateis for this task!

最简单的方法,evaluate就是为了这个任务!

The simplest way to obtain a value is by evaluate()method:

获取值的最简单方法是通过evaluate()方法

$xp = new DOMXPath($dom);
$v = $xp->evaluate("string(/etc[1]/@stringValue)");

Note: important to limit XPath returns to 1 item (the first ain this case), and castthe value with string()or round(), etc.

注意:将 XPath 限制为 1 项(a在本例中为第一项)并使用or等强制转换值很重要。string()round()



So, in a set of multiple items, using your foreachcode,

因此,在一组多个项目中,使用您的foreach代码,

 $names = $domXpath->query("//td[@class='name']/");
 foreach($names as $contextNode) {
    $text = $domXpath->evaluate("string(./a[1])",$contextNode);
    $href = $domXpath->evaluate("string(./a[1]/@href)",$contextNode);
 }

PS: this example is only for evaluate's illustration... When the information already exists at the node, use what offers best performance, as methods getAttribute(), saveXML(), etc. and properties as $nodeValue, $textContent, etc. supplied by DOMNode.
See @Gordon's answer for this particular problem.
The XPath subquery(at context) is good for complex cases — or symplify your code, avoiding to check hasChildNodes()+ loop for $childNodes, etc. with no significative gain in performance.

PS:这个例子只是为了evaluate的说明......当信息的节点,利用已经存在哪些优惠最佳的性能,如方法getAttribute()saveXML()等和性能$nodeValue$textContent的供应DOMNode
有关此特定问题,请参阅 @Gordon 的回答。
XPath的子查询(在上下文中)是复杂的情况下,良好的-或symplify你的代码,避免检查hasChildNodes()+循环为$的childNodes,等在性能无显着性收益。