5

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.

6
  • What if you don't pass the 0? That means "return the first item found" according to the documentation. Without the second parameter an array should be returned instead. Then you'd need to foreach through those results as well, however. Alternatively, $html2->find('ol.tracklist a'); might return an array of all the anchor tags within an ordered list with a class of "tracklist". Commented Apr 11, 2017 at 5:10
  • @stratedge here's the content I get if I var_dump($title) after changing it to the method you recommended, i.imgur.com/V29lp6r.png Commented Apr 11, 2017 at 5:23
  • What does your desired output look like? Or maybe what part of the Anchor do you need? Commented Apr 11, 2017 at 5:28
  • @stratedge I'm trying to get the download link where it says dopefile.pk, thanks for all the help btw! Commented Apr 11, 2017 at 5:29
  • The answer below from @Ghost is essentially what I was going to comment, looks like it should get you what you need Commented Apr 11, 2017 at 5:34

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Herbo just traverse it just like you'd normally do, its already presented in your markup, for each <li> tag, get the ul -> li -> a, the 0 represents the first element which is the one you want. glad this helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.