preg 替换所有链接

发布于 2024-08-21 23:53:20 字数 1295 浏览 6 评论 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";

我怎样才能彻底切断本文中的所有链接?

$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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-08-28 23:53:20

您可以尝试这样简单的操作:

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

不过,它会在结果字符串中留下双空格。你可以处理这个问题,但会变得稍微复杂一些。我也不检查删除的文本是否是有效的网址。任何包含 :// 的内容都会被删除。

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.

幻梦 2024-08-28 23:53:20

一种简单的方法:

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

将任何以“http”开头且包含非空格字符的字符串替换为空字符串。

这是假设您只获取 http。否则,就不那么天真了(但仍然大多天真):

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文