2012-06-16 26 views
1

可能重复:
Select xml node by attribute in phpgetElementByAttribute PHP DOM

有没有像在PHP getElementByAttribute任何功能?如果不是,我该如何创建解决方法?

E.g.

<div class="foo">FOO!</div> 

如何匹配该元素?

+0

此外http://stackoverflow.com/questions/699821/php-check-if-xml-node-exists-with-attribute –

+1

不同样,如果HTML不是XHTML,HTML可能比XML更复杂。 – Cranio

回答

2

您可以使用XPath:

$xpath = new DOMXPath($document); 
$results = $xpath->query("//*[@class='foo']"); 

Here's a demo.