Created
April 11, 2016 14:32
-
-
Save Foxy79/86df0a962d85262dc36a22f6ac5c6cd4 to your computer and use it in GitHub Desktop.
Форматирование номера телефона
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function format_phone($phone = '', $convert = false, $trim = true) { | |
if (empty($phone)) return ''; | |
$phone = preg_replace("/[^0-9A-Za-z]/", "", $phone); | |
if ($convert == true) { | |
$replace = array('2'=>array('a','b','c'),'3'=>array('d','e','f'),'4'=>array('g','h','i'),'5'=>array('j','k','l'),'6'=>array('m','n','o'),'7'=>array('p','q','r','s'),'8'=>array('t','u','v'), '9'=>array('w','x','y','z')); | |
foreach($replace as $digit=>$letters) { | |
$phone = str_ireplace($letters, $digit, $phone); | |
} | |
} | |
if ($trim == true && strlen($phone)>11) { | |
$phone = substr($phone, 0, 11); | |
} | |
// Perform phone number formatting here | |
if (strlen($phone) == 7) { | |
return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "$1-$2", $phone); | |
} elseif (strlen($phone) == 10) { | |
return preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "($1) $2-$3", $phone); | |
} elseif (strlen($phone) == 11) { | |
return preg_replace("/([0-9a-zA-Z]{4})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "($1) $2-$3", $phone); | |
} | |
return $phone; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment