Search code examples
phpregexpreg-match

Preg_replace range character class


It is saying this and I wonder why that happened. It didn't happen before.

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 16 in messenger.php on line 158

$pattern = array(
             "/[^@\s]*@[^@\s]*\.[^@\s]*/",
            "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i",
            '/\+?[0-9][0-9()-\s+]{4,20}[0-9]/'
        );
        $replacement    = array(
            "[removed email]",
            "[removed url]",
            "[removed phone]"
        );
        $message_text   = preg_replace($pattern, $replacement, $message_text);

Line 158 is the last one with $message_text.

Who can help?


Solution

  • You've forgotten to escape the dash in:

    '/\+?[0-9][0-9()-\s+]{4,20}[0-9]/'
    //       here __^
    

    should be:

    '/\+?[0-9][0-9()\-\s+]{4,20}[0-9]/'