I need to convert all characters to uppercase except for the last character in the following string:
<?php
$str = "Hello";
echo $_str = mb_strtoupper(mb_substr($str, 0, strtolower($str)));
?>
I need to convert all characters to uppercase except for the last character in the following string:
<?php
$str = "Hello";
echo $_str = mb_strtoupper(mb_substr($str, 0, strtolower($str)));
?>
2
Answers
Divide the string into the relevant parts, treat them accordingly, and then rejoin.
Output:
Here is an example of how you could accomplish this.
Where
substr($str, -1);
is grabbing the last character of the string and setting it to$last_char
.Where
substr_replace($str ,"",-1)
is replacing the last character from the string with "nothing".Where
strtoupper($str);
is capitalizing all the characters in the string.Where
$str . $last_char;
is re-adding the character to the end of the string using concatenation.OUTPUT: