Skip to content

Instantly share code, notes, and snippets.

@tkachev-o
Created August 9, 2018 10:58
Show Gist options
  • Save tkachev-o/a9c8299f5a7f866defd5cb400413f815 to your computer and use it in GitHub Desktop.
Save tkachev-o/a9c8299f5a7f866defd5cb400413f815 to your computer and use it in GitHub Desktop.
Простая функция для вывода форматированного номера телефона по маске
<?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