2012-06-14 71 views
1

我試圖從網站獲取一些圖像,但由於不同的文件擴展名已經得到難倒...PHP找出第二節比賽

我在preg_match_all()

if (preg_match_all('~http://foo.com/foo_(.*?).(.*?)~i', $returned_content, $matches)) { 
2場比賽

月1日是IMG名和第2個是IMG的擴展,並想找出每個分機是什麼,所以我可以稍後在代碼中使用它:

更新,全碼:

 //cURL function here get_data() 

$returned_content = get_data('http://foo.com/page/2'); 

     if (preg_match_all('~http://foo.com/foo_(.*?)\.(.*?)~i', $returned_content, $matches)) { 

    foreach ($matches[1] as $key) { 

    $file = 'http://foo.com/foo_' . $key . 'correct extension';// Need to have correct extension here 
     echo '<img src="' . $file . '" alt="" />'; 
      } 
     } 
+3

我假設你想通過轉義它來創建一個文字'.':'\ .'? – Wiseguy

+0

您可能需要將HTML讀取到HTML解析器中並選擇img src的值。正則表達式並不是真正解決這個問題的最好方法。 – dnagirl

回答

1
<?php 
$returned_content = "sdfasdfsdfshttp://foo.com/foo_sdfsdfsdf.fdfdsf"; 
preg_match_all('~http\://foo\.com/foo_(.*?)\.(.*?)$~i', $returned_content, $matches); 
print_r($matches); 

返回

Array 
(
[0] => Array 
    (
     [0] => http://foo.com/foo_sdfsdfsdf.fdfdsf 
    ) 

[1] => Array 
    (
     [0] => sdfsdfsdf 
    ) 

[2] => Array 
    (
     [0] => fdfdsf 
    ) 

) 

除非:和。在字符塊[],:和。被用作修飾符,它們必須被轉義。

+0

':'在這裏沒有特別的含義,所以沒有必要逃避它。 – cmbuckley