views:

320

answers:

2
$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";

How do I in this text completely cut all links?

+1  A: 

A naive approach:

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

Replaces any string that starts with "http" and consists of non-space characters, with an empty string.

This is assuming that you're only getting http. Otherwise, a little less naive (but still mostly naive):

preg_replace('#[a-z]+://[^\s]+#', "", $str)
Vivin Paliath
It's a good idea to assign the result to something to avoid confusion. A common question on SO is 'Why doesn't the replace method do anything?' or variations on that theme.
Mark Byers
Ahh, good point. Thanks for that tip :)
Vivin Paliath
+3  A: 

You could try something simple like this:

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

It will leave double spaces in the resulting string though. You could handle that, but it would get slightly more complicated. I also don't check if the text removed are valid URLs. Anything containing :// is removed.

Mark Byers