php: Remove URL from string

后端 未结 2 1656
走了就别回头了
走了就别回头了 2021-02-14 14:58

I have many strings (twitter tweets) from which I would like to remove the links when I echo them .

I have no control over the string and even though all the links star

2条回答
  •  抹茶落季
    2021-02-14 15:18

    If you want to remove everything, link and after the link, like via thing in your example, the below may help you:

    $string = "The Third Culture: The Frontline of Global Thinkinghttp://is.gd/qFioda;via @edge";
    $regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?).*$)@";
    echo preg_replace($regex, ' ', $string);
    

    If you want to keep them:

    $string = "The Third Culture: The Frontline of Global Thinkinghttp://is.gd/qFioda;via @edge";
    $regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@";
    echo preg_replace($regex, ' ', $string);
    

提交回复
热议问题