tags:

views:

58

answers:

2

Hello,

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-->

and:

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

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

A: 

Nothing immediately jumps out at me, though I guess if $gsmNumber contains a square bracket or backslash 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.

At the point at which it happens, print out the contents of the regex string (after $gsmNumber is applied) and work out where offset 54 of that string is.

thomasrutter
A: 

You probably have people insert mobile numbers including +, -, ( and/or ) characters and just use these as is in your preg_match, so you might want to sanitize the data provided before using it (ie. by stripping these characters out completely).

wimvds
Instead of stripping those characters you can also escape them, i.e. marking them as literal characters, see http://docs.php.net/preg_quote
VolkerK