Php 从07045开始的所有电话号码的Preg匹配

Php 从07045开始的所有电话号码的Preg匹配,php,forms,preg-match,Php,Forms,Preg Match,我试图添加一些代码,以防止在电话号码以07405开头时输入表单。我试过以下方法,但运气不好,有什么想法吗 HTML字段: <input type="tel" name="required[phone]" placeholder="Telephone Number" data-required="strict"> 干杯, Dan使用 preg_match('/^07045/',$phone); 使用 您忘记了preg_匹配处的分隔符,以及字符串的开头(您尝试在整个字

我试图添加一些代码,以防止在电话号码以07405开头时输入表单。我试过以下方法,但运气不好,有什么想法吗

HTML字段:

        <input type="tel" name="required[phone]" placeholder="Telephone Number" data-required="strict">
干杯, Dan使用

preg_match('/^07045/',$phone);
使用


您忘记了preg_匹配处的分隔符,以及字符串的开头(您尝试在整个字符串中匹配子字符串)

第二件事是这个任务不需要正则表达式,substr会更快

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

您忘记了preg_匹配处的分隔符,以及字符串的开头(您尝试在整个字符串中匹配子字符串)

第二件事是这个任务不需要正则表达式,substr会更快

if (substr($phone, 0, 5) == '07045')
为了避免使用正则表达式,您也可以使用:

if ( strpos( $phone, '07045' ) === 0 ) 
为了避免使用正则表达式,您也可以使用:

if ( strpos( $phone, '07045' ) === 0 ) 

您应该使用javascript或jQuery在客户端执行此检查。
对于php使用,
preg_match(“/^07405/”,(string)$phone)

您应该使用javascript或jQuery在客户端执行此检查。
对于php使用,
preg_match(“/^07405/”,(string)$phone)

您肯定没有阅读手册:您肯定没有阅读手册:谢谢!我按照建议使用了substr,工作如梦。谢谢!我按照建议使用substr,工作如梦。