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)));
?>
CodePudding user response:
Divide the string into the relevant parts, treat them accordingly, and then rejoin.
$str = "Hello";
$parts = mb_str_split($str, mb_strlen($str) - 1);
$out = mb_strtoupper($parts[0]) . mb_strtolower($parts[1]);
var_dump($out);
Output:
string(5) "HELLo"
CodePudding user response:
Here is an example of how you could accomplish this.
$str = "Hello";
$last_char = substr($str, -1);
$str = substr_replace($str ,"",-1);
$str = strtoupper($str);
$str = $str . $last_char;
echo $str;
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:
string(5) "HELLo"