Remove URLs from text string

前端 未结 3 1253
滥情空心
滥情空心 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:43

    You can easily use a regex to find the URLs, then specify what to replace them with using PHP's function preg_replace.

    http://daringfireball.net/2010/07/improved_regex_for_matching_urls

    Edit: Since this is user submitted data, you might want to do some validation before you store the "description" field, and check to see if it contains a URL. If it does, you can prevent the user from saving the form.

    For this, you can use preg_match, while still using a regex to find a URL.

提交回复
热议问题