问题
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.
回答1:
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.)
回答2:
if (preg_match('/[^a-zA-Z0-9_\.-]/', $_POST['txtEmail']))
if (preg_match('/[^a-zA-Z0-9:\?\.-]/', $_POST['txtSubject']))
来源:https://stackoverflow.com/questions/7859102/php-5-2-preg-match-compilation-failure