2013-12-12 37 views
0

這是我的正則表達式來獲取頁面上的圖像網址。從頁面源代碼解析圖像url

<?php  
     $url = $_POST['url'];  
     $data = file_get_contents($url);  
     $logo = get_logo($data); 
     function get_logo($html) 
      { 
       preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches); 
       //echo "mactch : $matches[0][0]"; 
       return $matches[0][0]; 
      } 

?> 

在正則表達式中是否缺少任何東西?對於一些網址,雖然他們有圖像,但它並沒有給出圖像網址。

例如:http://www.milanart.in/

它不給該網頁上的圖像。

請沒有圓頂。我無法使用它。 PHP的

+0

可能重複的[你如何解析和處理PHP中的HTML/XML?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and- process-html-xml-in-php) – Quentin

回答

1
<?php  
    $url = "http://www.milanart.in";  
    $data = file_get_contents($url); 
    $logo = get_logo($data); 

    function get_logo($html) 
     { 
      preg_match_all("/<img src=\"(.*?)\"/", $html, $matches); 
      return $matches[1][0]; 
     } 
    echo 'logo path : '.$logo; 
    echo '<img src="'.$url.'/'.$logo.'" />'; 
?> 
+0

謝謝,但這種污染並不適用於所有人案件。正則表達式應該是這樣的,它應該獨立地報廢圖像url。檢查您的上述代碼爲'http://www.metacritic.com/movie/walter-lessons-from-the-worlds-oldest-people/critic-reviews' – user123

+0

這是一個與一個解決方案工作的例子,你可以返回一個數組與所有響應,並檢查該字符串是否有'http'或不...您需要修改您的代碼 – keegzer

1

使用DOM類來獲得所有圖片:

  1. 搜索CSS ..... URL(imagefilename.extension)
  2. 搜索HTML圖像文件的圖像文件。 .....
+0

由於我的代碼中存在一些問題,我無法使用dom。 – user123