Created
November 18, 2017 03:41
-
-
Save nasrulhazim/df8f4046cceb37ab61fa5d5c0c65dba6 to your computer and use it in GitHub Desktop.
Generate Abbreviation Based on Given String
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 | |
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