2012-04-09 154 views
-1

我有這樣的字符串:我想只有第一個圖像鏈接(這個詞後,「小」),並忽略所有其他(單詞「boxshot」和如何過濾字符串

"small: 
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg 
boxshot: 
http://img.exent.com/free/frg/products/666550/boxshot.jpg" 

之後的鏈接)。

我該怎麼做?

+1

您遇到的問題是什麼? – mishu 2012-04-09 09:05:21

+0

歡迎來到StackOverflow。您可以使用工具欄按鈕格式化源代碼。這次我爲你做了。 – 2012-04-09 09:05:47

+0

[從字符串獲取網址]的可能的重複(http://stackoverflow.com/questions/1146198/get-a-url-from-a-string) – JJJ 2012-04-09 09:08:58

回答

0

最簡單的辦法:

<?php 
    $str='small: http://img.exent.com/free/frg/products/666550/player_boxshot.jpg boxshot: http://img.exent.com/free/frg/products/666550/boxshot.jpg'; 
    $parts=explode('boxshot:',$str); 
    $small=$parts[0]; 
    echo($small); 
?> 
1

使用它,像我的答案。 string是你的上面整串

substr(string,0,stripos(string,"boxshot")-1); 
+0

這是我如何做到這一點,除非不從0開始,假設'小:'不會改變,你想從6開始。 – 2012-04-09 09:12:57

+0

停止說**像我的答案..就像我的答案.. * *這不是Facebook。 – 2012-04-10 10:29:51

1
$string = "small: 
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg 
boxshot: 
http://img.exent.com/free/frg/products/666550/boxshot.jpg"; 

preg_match_all('~http(.*?)jpg~i',trim($string),$matches); 
var_dump($matches[0][0]); 
0

試試這個:

$string = "small: 
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg 
boxshot: 
http://img.exent.com/free/frg/products/666550/boxshot.jpg"; 

// replace all 2 or more consecutive spaces with 1 space 
$string = preg_replace('/\s\s+/i', ' ', $string); 
$array = explode(' ', $string); 
echo $array[1]; 

希望這有助於。