match url pattern in php using regular expression

前端 未结 8 1698
别那么骄傲
别那么骄傲 2020-12-01 18:32

I want to match a url link in wall post and replace this link with anchor tag, for this I use the regular expression below.

I would like the match 4 types of url:

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 19:21

    I'd use a different regex to be honest. Like this one that Gruber posted in 2009:

    \b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
    

    or this updated version that Gruber posted in 2010 (thanks, @IMSoP):

    (?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
    

提交回复
热议问题