2010-01-12 158 views

回答

2
$string = 'hello http://dsqdsq.com/fdsfsd?fsdflsd enjoy !'; 
$stripped_string = preg_replace('; ((ftp|https?)://|www3?\.).+? ;', ' ', $string); 

更新:這裏是\ B的,而不是空間,這將更好地工作。非常感謝cletus!

$string = 'hello http://dsqdsq.com/fdsfsd?fsdflsd enjoy !'; 
$stripped_string = preg_replace(';\b((ftp|https?)://|www3?\.).+?\b;', ' ', $string); 
+0

不要忘記FTP:// – 2010-01-12 20:46:47

+0

這是否需要在開始的空間 - 即行啓動與URL不匹配? – Simon 2010-01-12 20:47:40

0

這樣的事情?只是把我的頭,不檢查

/(http:\/\/(.*?)\s)/i 
3

前我會做這種方式:

$output = preg_replace('!\b((https?|ftp)://)?www3?\..*?\b!', '', $input); 

其中:

  • 開始在單詞邊界(\b);
  • 可選擇以http://,https://或ftp://開頭;和
  • 有一個以www開頭的域名。或www3。

然後刪除所有到下一個單詞邊界的文本。使用\b通常優於檢查空格。 \b是一個零寬度(意思是它不佔用輸入的任何部分),匹配字符串的開始,字符串的結尾,從單詞到非單詞字符的轉換或從非單詞轉換爲一個字的字符。

+0

+1 \ b!真的很好的運營商知道!你每天學習新的東西! – 2010-01-12 20:51:03

0

嗯..嘗試從克萊

$pattern=array(
     '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', 
     '`((?<!//)(www\.\S+[[:alnum:]]/?))`si' 
     ); 
$output = "http://$1"; 
$input = // set some url here; 
preg_replace($pattern,$output,$input); 
0

解決方案不能正常工作,因爲。 (點)也是單詞邊界,所以我們應該用空白標記\ s,而不是字bouadry底:

$output = preg_replace('!\b((https?|ftp)://)?www3?\..*?\s!', '', $input); 
相關問題