php - Looping Through Multiple HTML Elements with DOMDocument
I have a page that looks something like this:
... <div > <div > <h3>Info 1</h3> <span >Title for Info 1</span> <a href="http://www.example.com/1">Link to Example 1</a> </div> <!-- /info --> <div > <h3>Info 2</h3> <span >Title for Info 2</span> <a href="http://www.example.com/2">Link to Example 2</a> </div> <!-- /info --> <div > <h3>Info 3</h3> <span >Title for Info 3</span> <a href="http://www.example.com/3">Link to Example 3</a> </div> <!-- /info --> </div> <!-- /container --> ...
The structure of each of the info class divs is the same, and I'd like to be able to loop through the document and for each div with a class of info, parse the various component into either an array or individual variables for the purposes of outputting the data in some sort of human-readable format, like a csv file or HTML table.
I've tried using the DOMDocument method, and using getElementByTagName to extract the contents of each tag, but because the div contains multiple tag types (h3, a, span), I haven't figure out how to accomplish what I'm looking to do.
In the end, I want to be able to put the data in a format like this:
divclass, h3, spanclass, spantitle, ahref, a info, Info 1, title, Title for Info 1, http://www.example.com/1, Link to Example 1 ...