Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeinnordeste/e071857148084da94891 to your computer and use it in GitHub Desktop.
Save madeinnordeste/e071857148084da94891 to your computer and use it in GitHub Desktop.
PHP - Remove URLs from string
<?php
$string = 'Hi, visit my website: http://beto.euqueroserummacaco.com';
$string = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $string);
echo $string;
?>
@andreszs
Copy link

andreszs commented Feb 8, 2018

If your URL begins simply with www and no protocol, modify it like this to filter it:
$string = preg_replace('/\b((https?|ftp|file):\/\/|www\.)[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', ' ', $string);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment