If I have an HTML document, what is the best way to go through the document and gather the href values from all anchor tags with a particular class using PHP?
Asked
Active
Viewed 1,490 times
0
-
almost a duplicate of [Preg_match_all – Gordon Jul 12 '10 at 21:19
-
If you want to query an XML document, you can use XPath. And HTML is not far from being an XML (you can tidy it if it is not, with... tidy). The SimpleXML extension [provides such a functionality](http://php.net/manual/en/simplexmlelement.xpath.php) – greg0ire Jul 12 '10 at 20:59
1 Answers
2
See soulmerge's answer to "Preg_match_all href". Adjust the XPath to
//a[@class="foo"]/@href
to get a DOMNodeList of all href attributes belonging to elements with a class of foo
.