2011-10-30 2 views
1

私は次のコードでimdbを掻き集めようとしていました。simple_html_domライブラリのPHP正規表現

$url = "http://www.imdb.com/search/title?languages=en|1&explore=year"; 
$html = new simple_html_dom(); 
$html->load(str_replace(' ','',$data = get_data($url))); 

foreach($html->find('#left') as $total_movies) 
{ 
$content = $total_movies->plaintext; 
if(preg_match("/(?<total>[0-9,]+) titles/",$content,$matches)) 
{ 
    print_r($matches); 
} 
echo $content."<br>"; 
} 

get_data()は作成したカール関数です。

問題は、preg_matchが機能していないことです。私は理由は分かっていませんが、ここで働くときは同じことがあります。 $ contentには、上記のコードでスクラップしたテキストが含まれています。

$content = "1-50 of 101 titles."; 
if(preg_match("/(?<total>[0-9,]+) titles/",$content,$matches)) 
print_r($matches); 

答えて

1

サイト上のソースは、実際には次のとおりです。

<div id="left"> 
1-50 of 564,592 
titles. 
</div> 

予告\nこれは取り去る必要があるか、あなたの条件に追加します。

Heres追加された追加ライブラリを使用せずに目標に達する方法。

<?php 
    $url = "http://www.imdb.com/search/title?languages=en|1&explore=year"; 
    $temp=file_get_contents($url); 

    $xml = new DOMDocument(); 
    @$xml->loadHTML($temp); 

    foreach($xml->getElementsByTagName('div') as $div) { 
     if($div->getAttribute('id')=='left'){ 
      preg_match("#of ([0-9,]+)#",$div->nodeValue,$match); 
      $matchs[]=preg_replace('/[^0-9]/', '', $match[0]); 
     } 
    } 

    echo number_format($matchs[0]); //564,592 

    ?> 
+0

ありがとうございました。 – user1020363