Dario Dario - 1 year ago 57
PHP Question

Range out of order in character class

I'm getting this odd error in the preg_match() function:

Warning: preg_match(): Compilation failed: range out of order in character class at offset 54

The line which is causing this is:

preg_match("/<!--GSM\sPER\sNUMBER\s-\s$gsmNumber\s-\sSTART-->(.*)<!--GSM\sPER\sNUMBER\s-\s$gsmNumber\s-\sEND-->/s", $fileData, $matches);

What this regular expression does is parse an HTML file, extracting only the part between:

<!--GSM PER NUMBER - 5550101 - START-->


<!--GSM PER NUMBER - 5550101 - END-->

Do you have a hint about what could be causing this error?


If $gsmNumber contains a square bracket, backslash or various other special characters it might trigger this error. If that's possible, you might want to validate that to make sure it actually is a number before this point.

Edit 2016:

There exists a PHP function that can escape special characters inside regular expressions: preg_quote().

Use it like this:

  '/<!--GSM\sPER\sNUMBER\s-\s' .
  preg_quote($gsmNumber, '/') . '\s-\sSTART-->(.*)<!--GSM\sPER\sNUMBER\s-\s' .
  preg_quote($gsmNumber, '/') . '\s-\sEND-->/s', $fileData, $matches);

Obviously in this case because you've used the same string twice you could assign the quoted version to a variable first and re-use that.