Error in regex

I’m getting the following warning message:

[Mon Apr 17 12:45:25 2017] mod_fcgid: stderr: PHP Warning: preg_match(): Compilation failed: invalid range in character class at offset 20 in /home/www/dev/login/admin/adminprocess.php on line 196

Line 196 is

!preg_match("/^[a-z0-9]([0-9a-z_-\s])+$/i", $subuser)

and I think character 20 is the minus sign. Is it just a question of escaping the minus sign or is there a better way to resolve the error?

You have to escape the minus sign, unless it is the last character in the range.

So either of these should solve your issue:

!preg_match("/^[a-z0-9]([0-9a-z_\-\s])+$/i", $subuser)
!preg_match("/^[a-z0-9]([0-9a-z_\s-])+$/i", $subuser)

Thanks @cpradio. I thought that was the answer but wanted to check. It’s a script I have resurrected from a few years ago. Strange it seemed to work then…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.