开发者

preg replace all links

开发者 https://www.devze.com 2022-12-20 04:02 出处:网络
$text = \"Clip - http://depositfiles.com/files/8b5560fne Mp3 - 开发者_Python百科http://letitbit.net/download/4920.adaf494fbe2b15c34a4733f20/Madonna___The_Power_Of_Good_Bye.mp3.html
$text = "Clip - http://depositfiles.com/files/8b5560fne Mp3 - 
 开发者_Python百科   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?


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消