在PHP中验证英国电话号码 [英] Validating UK phone numbers in PHP

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

问题描述

我购买了一个联系表格。伟大的小东西,但我需要转换电话号码的验证,以允许英国数字格式 - 换句话说,允许空格。

现在它验证没有($ phone))

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b if(!$ phone || strlen($ phone)< 8)
{
$ error。=请输入您的电话号码,不得有空格。< br />



{
$ error。=请在电话号码栏中输入数字字符< br />;


解决方案

正则表达式模式,这是你将需要的。



这种模式,例如:

  $ pattern =/ ^(\ + 44 \s?7 \ {3} | \(?07 \ {3} \)?)\ s?\d {3} \s \d {3} $ /?; 

$ match = preg_match($ pattern,$ input);
$ b $ if($ match!= false){
//我们有一个有效的电话号码
} else {
//我们有一个无效的电话号码



$ b $ p
$ b $ p $这个模式可以匹配+44包含或不包含例如$ b $

$ $ $ $ $
$ b $(07222) 555555

+44 7222 555 555

这些不会

  7222 555555 

+44 07222 555555

(+447222)555555

正则表达式中提供了一些教程/备忘单等等的站点。 / p>

http://regexlib.com/Default.aspx

以及一个很好的堆栈溢出帖子:

一个全面的正则表达式的电话号码有效ation


I purchased a contact form. Great little thing but I need to convert the validation for the phone number to allow for UK number formats - in other words, to allow for spaces.

Right now it validates without spaces and has a minimum length of 8 characters:

if(is_numeric($phone))
{
    if(!$phone || strlen($phone) < 8)
    {
        $error .= "Please enter your phone number without spaces.<br />";
    }
}
else
{
    $error .= "Please enter numeric characters in the phone number field.<br />";
}

解决方案

Phone numbers are typically horrible for regex patterns, which is what you will need.

This pattern for example:

$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";

$match = preg_match($pattern,$input);

if ($match != false) {
    // We have a valid phone number
} else {
    // We have an invalid phone number
}

That pattern will match with +44 included or not e.g.

all these will match:

07222 555555

(07222) 555555

+44 7222 555 555

These won't

7222 555555

+44 07222 555555

(+447222) 555555

There are a load of sites that offer tutorials / cheat sheets etc. for regular expressions try some of these:

http://regexlib.com/Default.aspx

as well as a very good stack overflow post:

A comprehensive regex for phone number validation

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

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