Created
August 9, 2018 10:58
-
-
Save tkachev-o/a9c8299f5a7f866defd5cb400413f815 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 | |
/** | |
* Простая функция для вывода форматированного номера телефона по маске | |
* | |
* | |
* @param string $phone | |
* @param string $pattern | |
* @return string | |
*/ | |
function phone_format($phone, $pattern = '+# (###) ### ##-##') { | |
$phone = preg_replace('/[^0-9]/', '', $phone); | |
$phone = str_split($phone); | |
$pattern = str_split($pattern); | |
$output = ''; | |
$i = 0; | |
foreach ($pattern as $value) { | |
if($value === '#') | |
$output .= $phone[$i++]; | |
else | |
$output .= $value; | |
} | |
return $output; | |
} | |
echo phone_format('79197055585', '+# (###) ###-##-##'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment