Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.2 preg_match() compilation failure

Would very much appreciate some help. My hosts server-side updates have caused my comment form to throw two validation errors. First one is: preg_match() returns Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 13 in [script location here and line error] - second is on subject check at offset 14.

It does this on the email address check:

if (preg_match('/[^a-zA-Z0-9_-.]/', $_POST['txtEmail']))

and subject check:

if (preg_match('/[^a-zA-Z0-9:?-. ]/', $_POST['txtSubject']))

I'm not familiar with PHP but can cut and paste! My website's frowey.com and it's the comments form on contact us that's started throwing an error after hosting OS updates. Thanks in advance.

like image 352
mochj Avatar asked Oct 22 '11 11:10

mochj


1 Answers

You need to escape the - minus. It has special meaning with character classes, as the error message hints. Use a backslash before the minus:

preg_match('/[^a-zA-Z0-9_\-.]/'

(Alternatively the - may be the first or last thing in the character group, so it loses its special function.)

like image 155
mario Avatar answered Sep 18 '22 15:09

mario