Regex PHP - Auto-detect YouTube, image and “regular” links

前端 未结 3 482
情话喂你
情话喂你 2020-12-17 04:47

I want to make it so that in my chat-application, links to websites can be clickable and links to YouTube and images automatically gets embedded.

I\'ve made this cod

3条回答
  •  时光说笑
    2020-12-17 05:27

    I had some issues with the preg_replace_callback when the text included three dots .... The above code recognized the three dots as a URL which is not true.

    Here is my fix and it seems to work at the moment $str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:jpe?g|png|gif)#', function($arr)

    Would this fix fail in other cases???

提交回复
热议问题