Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore regex if part of a URL?

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!
like image 338
ProgrammerGirl Avatar asked Feb 01 '26 01:02

ProgrammerGirl


1 Answers

I think you have to parse the url AND the phone number, like /(?: url \K | phone number)/ - sln
@sln: How would I do that? If it helps, there is a URL regex here: stackoverflow.com/a/8234912/869849 – ProgrammerGirl

Here is an example using the provided regex for url and phone num:

Php test case

 $text = 'Please help me with my +44-83848-1234 question here: http://stackoverflow.com/+44-83848-1234questions/20589314/ phone #:+44-83848-1234-Thanks!';
 $str = preg_replace_callback('~((?:(?:[a-zA-Z]{3,9}:(?://)?)(?:[;:&=+$,\w-]+@)?[a-zA-Z0-9.-]+|(?:www\.|[;:&=+$,\w-]+@)[a-zA-Z0-9.-]+)(?:(?:/[+\~%/.\w-]*)?\??[+=&;%@.\w-]*\#?\w*)?)|(\+?[0-9][0-9()\s+-]{4,20}[0-9])~',
                   function( $matches ){
                        if ( $matches[1] != "" ) {
                             return $matches[1];
                        }
                        return '[removed]';
                   },
                   $text);

 print $str;

Output >>

 Please help me with my [removed] question here: http://stackoverflow.com/+44-83848-1234questions/20589314/ phone #:[removed]-Thanks!

Regex, processed with RegexFormat

 # '~((?:(?:[a-zA-Z]{3,9}:(?://)?)(?:[;:&=+$,\w-]+@)?[a-zA-Z0-9.-]+|(?:www\.|[;:&=+$,\w-]+@)[a-zA-Z0-9.-]+)(?:(?:/[+\~%/.\w-]*)?\??[+=&;%@.\w-]*\#?\w*)?)|(\+?[0-9][0-9()\s+-]{4,20}[0-9])~'

     (                                  # (1 start), URL
          (?:
               (?:
                    [a-zA-Z]{3,9} :
                    (?: // )?
               )
               (?: [;:&=+$,\w-]+ @ )?
               [a-zA-Z0-9.-]+ 
            |  
               (?: www \. | [;:&=+$,\w-]+ @ )
               [a-zA-Z0-9.-]+ 
          )
          (?:
               (?: / [+~%/.\w-]* )?
               \??
               [+=&;%@.\w-]* 
               \#?
               \w* 
          )?
     )                                  # (1 end)
  |  
     (                                  # (2 start), Phone Num
          \+? 
          [0-9] 
          [0-9()\s+-]{4,20} 
          [0-9] 
     )                                  # (2 end)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!