How to format a phone number in php
How to format a phone number in php
This article describes how to format a phone number in PHP. Share it with everyone for your reference. The specific analysis is as follows:
This function is only applicable to US calls. Chinese calls need to be modified by yourself
?
1
2
3
4
5
6
7
8
9
10
|
function format_phone($phone)
{
$phone = preg_replace("/[^0-9]/", "", $phone);
if(strlen($phone) == 7)
return preg_replace("/([0-9]{3})([0-9]{4})/", "-", $phone);
elseif(strlen($phone) == 10)
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/","() -",$phone);
else
return $phone;
}
|
1
2
3
4
5
6
7
8
9
10
|
function format_phone($phone){
$phone = preg_replace("/[^0-9]/", "", $phone);
if(strlen($phone) == 7)
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
elseif(strlen($phone) == 10)
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/","($1) $2-$3", $phone);
else
return $phone;
}
|
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/989556.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/989556.htmlTechArticleHow to format a phone number in php How to format a phone number in php This article explains how to format a phone number in php method. Share it with everyone for your reference. The specific analysis is as follows: This...