gpt4 book ai didi

PHP 正则表达式无法正常工作

转载 作者:行者123 更新时间:2023-12-03 11:49:42 25 4
gpt4 key购买 nike

因此,我尝试使用 xxx-xxx-xxxx 形式验证电话号码,其中 x 是数字。我遇到的问题是我可以为最后一组数字输入超过 4 位数字。有没有人看到我做错了什么?谢谢。

    $telNumPattern = '/[0-9]{3}-[0-9]{3}-[0-9]{4}/'; //regular expression to match xxx-xxx-xxxx
if(empty($_POST['telNum'])) {
$errors['telNum'] = "*Your telephone number can't be empty";
}
else if(!preg_match($telNumPattern, $_POST['telNum'])) {
$errors['telNum'] = "*Your email must be in the form xxx-xxx-xxxx";
}

最佳答案

$telNumPattern = '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/';

使用 anchor 进行精确匹配。

关于PHP 正则表达式无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893842/

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