gpt4 book ai didi

php -
转载 作者:行者123 更新时间:2023-12-03 19:19:09 25 4
gpt4 key购买 nike

长期听众。第一次来电...

严格来说,这不是一个 PHP 问题,因为它涉及正则表达式,但这个问题让我抓狂。

我有 3 个要创建的正则表达式,只有一个可以正常工作。

现在我不确定这是不是因为:

  1. 我不明白 preg_match 和ereg 和他们的返回代码,因为我大约 7 年没有使用 PHP年。
  2. 我的正则表达式是完全错了。
  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 functionsPOSIX 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/

25 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com