如何使用PHP验证电话号码? [英] How to validate phone number using PHP?

查看:368
本文介绍了如何使用PHP验证电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用php验证电话号码

How to validate phone number using php

推荐答案

由于电话号码必须符合模式,因此您可以使用正则表达式将输入的电话号码与您在regexp中定义的模式进行匹配.

Since phone numbers must conform to a pattern, you can use regular expressions to match the entered phone number against the pattern you define in regexp.

php同时具有ereg和preg_match()函数.我建议使用preg_match(),因为有更多关于这种正则表达式样式的文档.

php has both ereg and preg_match() functions. I'd suggest using preg_match() as there's more documentation for this style of regex.

一个例子

$phone = '000-0000-0000';

if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone)) {
  // $phone is valid
}

这篇关于如何使用PHP验证电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆