Created
January 26, 2016 01:57
-
-
Save fdcore/05029aeba743650a15af 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 $full_name Полное ФИО | |
@param $format формат, поддержиюваются буквы A,B,C для полных слов и a,b,c для сокращений в 1 букву | |
@author Dmitriy Nyashkin | |
*/ | |
function full_name_to_short ($full_name, $format="A b. c.") { | |
$words = explode(" ", $full_name); | |
$format_keys = array("A", "B", "C"); | |
$short_name = $format; | |
foreach ($format_keys as $index => $word) { | |
$short_name = str_replace($word, $words[$index], $short_name); | |
$short_name = str_replace(mb_strtolower($word), mb_substr($words[$index], 0, 1, 'UTF-8'), $short_name); | |
} | |
return $short_name; | |
} | |
// DEMO | |
echo full_name_to_short("Пупкин Вася Иванович"); // Пупкин В. И. | |
echo full_name_to_short("Пупкин Вася Иванович", 'B C a.'); // Вася Иванович П. | |
echo full_name_to_short("Пупкин Вася Иванович", 'B C'); // Вася Иванович | |
echo full_name_to_short("Пупкин Вася Иванович", 'B A'); // Вася Пупкин |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment