tags:

views:

54

answers:

4

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
+1  A: 

Use DOM Document Parser.

http://php.net/manual/pl/book.dom.php

Svisstack
I am looking at this, and I don't see a way to get an element by class, only by ID.
jordanstephens
You can search for all elements by tag name, using `getElementsByTagName`, then filter this list using the `getAttribute` method with the `class` attribute.
Scharrels
A: 

how about some regex-ing

this certainly does not scale but...

it might just be a quick and dirty fix

niklasfi
+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