作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
长期听众。第一次来电...
严格来说,这不是一个 PHP 问题,因为它涉及正则表达式,但这个问题让我抓狂。
我有 3 个要创建的正则表达式,只有一个可以正常工作。
现在我不确定这是不是因为:
无论哪种方式,这里都是表达方式和我使它们起作用的微弱尝试。
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 或 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.";
}
如果有人能告诉我我做错了什么,我会给他们我的第一个 child 。
最佳答案
你搞混了 PCRE functions和 POSIX regular expression functions .您正在使用 Perl-Compatible regular expression具有 POSIX 正则表达式功能。
所以用 preg_match
替换 ereg
它应该可以工作:
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 语法错误外,我还为要匹配的字符串的开头 (^
) 和结尾 ($
) 添加了 anchor 。
关于php - <?PHP、REGEX 和我。三幕悲剧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/901558/
Hello,大家好,我是楼下小黑哥~ 我们本地使用 IDEA 运行 maven 项目的时候,有时候运气不好,就会遇到某些 maven 依赖无法正常找到、导入。这就会导致 IDEA
我是一名优秀的程序员,十分优秀!