Remove URLs from text string

前端 未结 3 1303
滥情空心
滥情空心 2021-01-22 00:07

Is it possible to search for and remove URLs from a string in PHP. Talking about the actual text here not the HTML. Example to remove:

mywebsite.com
http://myweb         


        
3条回答
  •  难免孤独
    2021-01-22 00:32

    This regex seems to do the trick:

    !\b(((ht|f)tp(s?))\://)?(www.|[a-z].)[a-z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-z0-9\.\,\;\?\\'\\\\\+&%\$#\=~_\-]+))*\b!i
    

    It is a slight modification of this regex from Regular Expression Library.

    I realize it’s a bit overwhelming, but that's to be expected when searching for URLs. Nevertheless, it matches everything on your list.

    Alternatively, you could loop through each word in the description and use parse_url() to see how the word breaks down. I’ll leave the criteria for determining if it's a url to you. There’s still the potential for false positives, but they could be greatly reduced. Combined with Andrew’s idea of flagging questionable content for moderation, it could be a workable solution.

提交回复
热议问题