0

I have this php codes:

$main_url = "http://www.sports-reference.com/olympics/countries/DEN/summer/1896/";
$main_html=file_get_html($main_url);

$link = $main_html->getElementById('div_sports');

    foreach ($link->find('td') as $element){


        foreach($element->find('href') as $node){

        echo $node->item(0)->nodeValue . "\n";
        //$link_clean = $node->getAttribute('href');
        echo $link_clean . "\n";

        }

    }

If I print out $element, I get this output:

<td align="left" ><a href="/olympics/countries/DEN/summer/1896/ATH/">Athletics</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/FEN/">Fencing</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/GYM/">Gymnastics</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/SHO/">Shooting</a></td>
<td align="left" ><a href="/olympics/countries/DEN/summer/1896/WLT/">Weightlifting</a></td>

I need to extract this info:

/olympics/countries/DEN/summer/1896/ATH/ /olympics/countries/DEN/summer/1896/FEN/ ..........

and so on. the code above is not working. CAn you helpme?

Idro
  • 253
  • 1
  • 7
  • There are multiple questions about this on stackoverflow. Here are two: http://stackoverflow.com/questions/4702987/php-string-manipulation-extract-hrefs http://stackoverflow.com/questions/6365701/php-extract-link-from-a-tag – Vincent Orback Feb 24 '16 at 23:18

1 Answers1

2

href is not a tag, it is a tag attribute.

So, you have to search for <a>:

foreach( $element->find('a') as $a)
{
    echo $a->href . "\n";
    (...)
}
fusion3k
  • 11,568
  • 4
  • 25
  • 47