Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Created November 18, 2017 03:41
Show Gist options
  • Save nasrulhazim/df8f4046cceb37ab61fa5d5c0c65dba6 to your computer and use it in GitHub Desktop.
Save nasrulhazim/df8f4046cceb37ab61fa5d5c0c65dba6 to your computer and use it in GitHub Desktop.
Generate Abbreviation Based on Given String
<?php
if (!function_exists('abbrv')) {
function abbrv($value)
{
$removeNonAlphanumeric = preg_replace("/[^A-Za-z0-9 ]/", '', $value);
$removeVowels = str_replace(
['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', ' '],
'',
$removeNonAlphanumeric);
$uppercase = strtoupper($removeVowels);
$split = str_split($uppercase);
$unique_characters = [];
foreach ($split as $character) {
if (!in_array($character, $unique_characters)) {
$unique_characters[] = $character;
}
}
return implode('', $unique_characters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment