-1
 function replaceQueryParams($string)
{
    $string = preg_replace_callback(
        '/\[\[([A-Za-z0-9_-\s]*)\]\]:(.*?):([10]*)/',
        array($this, 'getQueryParamsMatches'),
        $string
    );
    return $string;
}

When i tried to run the below code it throws error.

Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
vikash
  • 1

1 Answers1

0

You need to escape dashes when using them inside of character classes:

'/[[([A-Za-z0-9_\-\s])]]:(.?):([10]*)/'

Quoting from this answer by Konrad Rudolph:

The hyphen is usually a normal character in regular expressions. Only if it’s in a group expression and between two other characters does it take a special meaning.

The crux of the issue is that the regular expression is believing that this part of the character class: _-\s is denoting a character range (like as in [A-Z] or [a-z]) which is invalid, hence the error.

The regex engine does not treat the hyphen as part of a character range if it is included at either the beginning or end of the character class, so both of these would also work for you:

'/[[([A-Za-z0-9_\s-])]]:(.?):([10]*)/'
'/[[([-A-Za-z0-9_\s])]]:(.?):([10]*)/'
Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96