2011-01-06 4 views
2

米国の大学やカレッジのデータベースのhtmlページを解析しようとしています。私が書いたコードは大学の名前を取得しますが、それぞれのURLアドレスを取得できません。<a>タグの属性値をPHPで解析する方法

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

これは私の現在のコードです。

属性値も同様に取得する方法を教えてください。

はありがとう

答えて

5

あなたは要素への参照を持っている場合は、ちょうどそう、おそらく、getAttribute()を使用する必要があります。

echo "$count:".$cols->item(0)->getAttribute('href') . "\n"; 
関連する問題