2012-03-04 83 views
0

如何從$avatar數組中檢索結果?
這是我的嘗試:
我希望得到以下(URL):
代碼:關於php的幾個問題preg_match_all

<div id="fotoprofilo"><img src="http://images.instagram.com/profiles/profile_19347213_75sq_0000.jpg"></div> 

和PHP:

$s2 = file_get_contents("http://followgram.me/user"); 
    $avatar = ""; 
    preg_match_all('/fotoprofilo\s*"><img src=".*\b">/', $s2, $avatar); 
    print_r($avatar[0]); 

1)如何是結果的工作?
2)它會找到它嗎? (因爲我看不到價值

在此先感謝。

+5

[不要使用正則表達式來解析HTML](http://stackoverflow.com/q/1732348/112968) – knittl 2012-03-04 15:19:44

+0

此外,您的服務器必須配置爲允許file_get_contents通過http: //默認情況下,它只能用於本地文件 – knittl 2012-03-04 15:21:48

+0

#knittl它實際上適用於我:)(http),我正在檢查不使用...另外,結果如何工作?! – funerr 2012-03-04 15:24:38

回答

1

使用的子表達式的數組和支架:

$s2 = file_get_contents("http://followgram.me/user"); 
$avatar = array(); 
preg_match_all('/fotoprofilo\s*"><img src="(.*)">/', $s2, $avatar); 
print_r($avatar); 

$頭像[1]應該包含您的網址。

要測試正則表達式和preg_match_all,您可以使用http://reghex.mgvmedia.com/。選擇「PHP」作爲語言,並選中「全局搜索」選項。工作正常:)