Search code examples
phphtmlsimple-html-dom

Get all HTML list element using Simple HTML Dom


Currently I am working on a project which requires me to parse some data from an alternative website, and I'm having some issues (note I am very new to PHP coding.)

Here's the code I am using below + the content it returns.

$dl = $html2->find('ol.tracklist',0);
print $dl = $dl->outertext;

The above code returns the data for what we're trying to get, it's below but extremely messy provided you would like to see click here.

However, when I put this in a foreach, it only returns one of the a href attributes at a time.

foreach($html2->find('ol.tracklist') as $li) 
{
    $title = $li->find('a',0);
    print $title;
}

What can I do so that it returns all of the a href elements from the example code above?

NOTE: I am using simple_html_dom.php for this.


Solution

  • Based on the markup, just point directly to it, just get it list then point to its anchor:

    foreach ($html2->find('ol.tracklist li') as $li) {
        $anchor = $li->find('ul li a', 0);
        echo $anchor->href; // and other attributes
    }