2012-12-01 35 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]; 
相关问题