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?
A:
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
greg0ire
2010-07-12 20:59:35
I am looking at this, and I don't see a way to get an element by class, only by ID.
jordanstephens
2010-07-12 21:04:16
You can search for all elements by tag name, using `getElementsByTagName`, then filter this list using the `getAttribute` method with the `class` attribute.
Scharrels
2010-07-12 21:08:52
A:
how about some regex-ing
this certainly does not scale but...
it might just be a quick and dirty fix
niklasfi
2010-07-12 21:06:51
+2
A:
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
.
Gordon
2010-07-12 21:19:11