Skip to content

Instantly share code, notes, and snippets.

@johnsage25
Created October 15, 2019 22:58
Show Gist options
  • Save johnsage25/0478b82ace3c2d36a1e09348a08de7f8 to your computer and use it in GitHub Desktop.
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.
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