Range out of order in character class

前端 未结 7 1583
广开言路
广开言路 2020-12-07 00:20

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 l

相关标签:
7条回答
  • 2020-12-07 01:11

    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:

    preg_match(
      '/<!--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.

    0 讨论(0)
提交回复
热议问题