Created
October 2, 2020 14:26
-
-
Save giangmd/97e8371db4a462c43f428e2f1b813ff0 to your computer and use it in GitHub Desktop.
Last word in string to uppercase with PHP
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 last_word_uppercase($sentence, $delimiter = ' ') { | |
$words = explode($delimiter, $sentence); | |
if (!empty($words)) { | |
$lastWord = array_pop($words); | |
$words[] = strtoupper($lastWord); | |
return implode($delimiter, $words); | |
} | |
return strtoupper($sentence); | |
} | |
$str = 'This is a sentence.'; | |
echo last_word_uppercase($str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment