Home >Backend Development >PHP Tutorial >How to format phone number in php_PHP tutorial

How to format phone number in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:14885browse

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

45 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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn