Created
October 15, 2019 22:58
-
-
Save johnsage25/0478b82ace3c2d36a1e09348a08de7f8 to your computer and use it in GitHub Desktop.
Abbreviate a string using PHP. This function returns a string containing only the first letters of each word in an input string capitalized.
This file contains 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
function abbreviate($string){ | |
$abbreviation = ""; | |
$string = ucwords($string); | |
$words = explode(" ", "$string"); | |
foreach($words as $word){ | |
$abbreviation .= $word[0]; | |
} | |
return $abbreviation; | |
} | |
/* | |
For example: | |
echo abbreviate("Hello World"); | |
Output: HW | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment