2010-02-16 153 views
0
$text = "Clip - http://depositfiles.com/files/8b5560fne Mp3 - 
    http://letitbit.net/download/4920.adaf494fbe2b15c34a4733f20/Madonna___The_Power_Of_Good_Bye.mp3.html 
    Madonna - The power of goodbye Your heart is not open, so I must go The spell has been broken...I 
    loved you so Freedom comes when you learn to let go Creation comes when you learn to say no You were 
    my lesson I had to learn I was your fortress you had to burn Pain is a warning that something's wrong 
    I pray to God that it won't be long Do ya wanna go higher? Chorus: There's nothing left to try 
    There's no place left to hide There's no greater power than the power of good-bye Your heart is not 
    open, so I must go The spell has been broken...I loved you so You were my lesson I had to learn I was 
    your fortress Chorus: There's nothing left to lose There's no more heart to bruise There's no greater 
    power than the power of good-bye Bridge: Learn to say good-bye I yearn to say good-bye Chorus: There's 
    nothing left to try There's no more places to hide There's no greater power than the power of good-bye 
    There's nothing left to lose There's no more heart to bruise There's no greater power than the power 
    of good-bye"; 

如何在本文中完全切斷所有鏈接?preg替換所有鏈接

+1

通過cut,你的意思是完全刪除並替換爲空字符串,而不記得你刪除了什麼? – 2010-02-16 22:22:24

回答

3

你可以嘗試一些簡單的像這樣:

$text = preg_replace("#\S+://\S+#", "", $text); 

將得到的字符串中離開雙空間雖然。你可以處理,但會稍微複雜一些。我也不檢查刪除的文本是否有效的URL。任何包含://被刪除。

1

天真的方法:

preg_replace('/http[^\s]+/', "", $str) 

替換以「http」開始,由非空格字符,用一個空字符串的任何字符串。

這是假設你只得到http。否則,少一點天真(但仍主要是幼稚):

preg_replace('#[a-z]+://[^\s]+#', "", $str) 
+0

將結果分配給某些內容以避免混淆是一個好主意。關於SO的常見問題是'爲什麼替代方法不做任何事情?或該主題的變化。 – 2010-02-16 22:27:13

+0

啊,好點。感謝您的提示:) – 2010-02-16 22:30:04