Preg match所有以07045开头的电话号码

Z0idberg

我正在尝试添加一些代码,以防止电话号码以07405开头的表单输入。我已经尝试了以下方法,但运气不好,有什么主意吗?

HTML栏位:

        <input type="tel" name="required[phone]" placeholder="Telephone Number" data-required="strict">

PHP:

  case 'phone';

           $phone = (sanitize_text_field($fields['phone']));
          if (preg_match('07045', $phone))
          {

            $fields['valid_phone'] = $phone;
            unset($fields['phone']);
          }
          else
          {
            unset($fields['phone']);
            array_push($errors, 'phone');
          }
          break;

干杯,丹

帕维尔

您在preg_match和字符串的开头忘记了定界符(尝试在整个字符串中匹配子字符串)。

preg_match('~^07045~', $phone)

第二件事是正则表达式对于此任务不是必需的,substr会更快。

if (substr($phone, 0, 5) == '07045')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章