PHP 5.2 preg_match() compilation failure

你离开我真会死。 提交于 2019-12-07 06:24:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!