2012-12-01 45 views

回答

3

將提取字符串的單個網址:

$string = 'some random text then url http://www.example.com/ and more text'; 

preg_match('!https?://[\w+&@#/%?=~|\!\:,.;-]*[\w+&@#/%=~|-]!', $string, $match); 

echo $match[0]; 

將提取的字符串多個網址變成一個數組:

$string = 'put some text http://stackoverflow.com/something/something.php some random text then url http://www.example.com/ and more text'; 

preg_match_all('!https?://[\w+&@#/%?=~|\!\:,.;-]*[\w+&@#/%=~|-]!', $string, $match); 

print_r($match[0]); 

所以基本上「刪除除URL之外的所有內容,然後將字符串設置爲匹配值:

$string = $match[0]; 
相關問題