On one of my PHP sites, I use this regular expression to automatically remove phone numbers from strings:
$text = preg_replace('/\+?[0-9][0-9()-\s+]{4,20}[0-9]/', '[removed]', $text);
However, when users post long URL's that contain several numbers as part of their text, the URL also gets affected by the preg_replace
, which breaks the URL.
How can I ensure the above preg_replace
does not alter URLs contained in $text
?
EDIT:
As requested, here is an example of a URL being broken by the preg_replace
above:
$text = 'Please help me with my question here: https://stackoverflow.com/questions/20589314/ Thanks!';
$text = preg_replace('/\+?[0-9][0-9()-\s+]{4,20}[0-9]/', '[removed]', $text);
echo $text;
//echoes: Please help me with my question here: https://stackoverflow.com/questions/[removed]/ Thanks!