Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取href属性_Php - Fatal编程技术网

Php 获取href属性

Php 获取href属性,php,Php,我有以下php代码: $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){ foreac

我有以下php代码:

$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";

        }

    }
如果我打印出$element,我会得到以下输出:

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

我需要提取以下信息:

/奥运会/国家/丹麦/夏季/1896/ATH/ /奥运会/国家/书斋/夏季/1896年/分/


等等。上面的代码不起作用。你能帮我吗?

href
不是标记,它是标记属性

因此,您必须搜索


href
不是标记,而是标记属性

因此,您必须搜索


关于stackoverflow,有多个问题。这里有两个:关于stackoverflow,有很多问题。这里有两个:
foreach( $element->find('a') as $a)
{
    echo $a->href . "\n";
    (...)
}