很长时间的听众。第一次打电话..。
这不是一个严格意义上的PHP问题,因为它涉及正则表达式,但这一个问题让我很生气。
我有三个正则表达式,我想要创建,只有一个是正确的工作。
现在我不知道这是否是因为:
不管是哪种方式,这都是表达方式和我无力的努力使它们发挥作用的原因。
1)匹配以2、3、4或5开头的任何数字,然后后面跟着5位数字。(我觉得这个很管用)
代码:
if (!ereg('/[2-5]\d{5}/', $_POST['packageNumber' )
{
echo "The package number is not the correct format.";
}2)匹配以2、3、4或5开头的任何数字,然后后面跟着5位数,然后是一个句号,然后是1或a 2。
if (!ereg("/[2-5]\d{5}\.[1-2]/", $_POST['packageModifier' )
{
echo "The package modifier is not the correct format.";
}3)匹配任何字母数字、空格、句点和下线最多50个字符的组合。
if (!ereg("/[0-9a-zA-Z\s\-\.]{0,50}/", $_POST['customerNumber' )
{
echo "The customer number is not the correct format.";
}如果有人能告诉我我做错了什么,我会给他们我的第一个孩子。
发布于 2009-05-23 13:59:44
我假设您错过了$_POSTS上的闭幕式],我为行的开始和结束添加了锚,并使用了preg_match。
如果您不锚定它,并且模式在字符串中的任何位置都匹配,那么整个东西就会匹配。例如。
如果第一个没有锚定,"dfasfasfasfasf25555555as5f15sdsdasdsfghfsgihfughd54“将被匹配。
1号
if (!preg_match('/^[2-5]\d{5}$/', $_POST['packageNumber'])) {
echo "The package number is not the correct format.";
}2号
if (!preg_match('/^[2-5]\d{5}\.[2-5]$/', $_POST['packageModifier'])) {
echo "The package modifier is not the correct format.";
}3号
if (!preg_match('/^[0-9a-zA-Z\s\-.]{0,50}$/m', $_POST['customerNumber'])) {
echo "The package modifier is not the correct format.";
}发布于 2009-05-23 13:52:51
你把PCRE函数和POSIX正则表达式函数混在一起了。您使用的是带有POSIX正则表达式函数的与Perl兼容的正则表达式。
因此,将ereg替换为preg_match,它应该可以工作:
if (!preg_match('/^[2-5]\d{5}$/', $_POST['packageNumber'])) {
echo "The package number is not the correct format.";
}
if (!preg_match("/^[2-5]\d{5}\.[1-2]$/", $_POST['packageModifier'])) {
echo "The package modifier is not the correct format.";
}
if (!preg_match("/^[0-9a-zA-Z\s\-.]{0,50}$/", $_POST['customerNumber'])) {
echo "The customer number is not the correct format.";
}在修复PHP语法错误的同时,我为要匹配的字符串的开始(^)和结束($)添加了锚。
发布于 2009-05-23 13:53:54
不需要锚定正则表达式吗?
其他的‘11111111111112111111111’将匹配/2-5\d{5}/。
https://stackoverflow.com/questions/901558
复制相似问题