如何在PHP中解析<a>标签的属性值

Zee*_*ang 2 php parsing dom html-parsing

我正在尝试为美国的大学和学院解析数据库的html页面.我写的代码确实提取了大学的名称,但我无法获取他们各自的网址.

public function fetch_universities()
{
    $url = "http://www.utexas.edu/world/univ/alpha/";   
    $dom = new DOMDocument();  
    $html = $dom->loadHTMLFile($url);  
    $dom->preserveWhiteSpace = false;   
    $tables = $dom->getElementsByTagName('table');   
    $tr = $tables->item(1)->getElementsByTagName('tr');
    $td =  $tr->item(7)->getElementsByTagName('td');  
    $rows =  $td->item(0)->getElementsByTagName('li');

    $count = 0;
    foreach ($rows as $row)   
    {   
        $count++;
        $cols = $row->getElementsByTagName('a');
        echo "$count:".$cols->item(0)->nodeValue. "\n";  
    }   
}
Run Code Online (Sandbox Code Playgroud)

这是我目前的代码.

请告诉我如何获取属性值.

谢谢

Fel*_*ing 5

如果你有一个元素的引用,你只需要使用getAttribute(),所以可能:

echo "$count:".$cols->item(0)->getAttribute('href') . "\n";
Run Code Online (Sandbox Code Playgroud)