2013-10-30 33 views
1

我已經follwing正則表達式來驗證從頁面獲取圖像:驗證的正則表達式導致

preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches); 

我想提出一些條件,如果匹配/不匹配。我能怎麼做?

現在我用得到的結果:

echo $matches[0][0]; 

但是,如果不匹配,意味着頁面上沒有圖像:

Notice: Undefined offset: 0 

我不知道如何驗證preg_match_all

的結果
+0

您是否嘗試過'print_r($ matches)',這樣您就可以看到數組的樣子了? – h2ooooooo

回答

3

只需檢查返回值preg_match_all函數。

$ret = preg_match_all('~\bhttps?://\S+(?:png|jpg)\b~i', $html, $matches); 

if ($ret) { 
    // matched 
    // use $matches array for the result 
} 
else { 
    // didn't match 
} 
+0

@Karimkhan:這個答案是否適合你? – anubhava