Skip to content

Instantly share code, notes, and snippets.

@fdcore
Created January 26, 2016 01:57
Show Gist options
  • Save fdcore/05029aeba743650a15af to your computer and use it in GitHub Desktop.
Save fdcore/05029aeba743650a15af to your computer and use it in GitHub Desktop.
Конвертирует полное ФИО в короткое сокращение. Можно менять последовательность слов вторым параметром.
<?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