如何将字符串的最后一个字母大写。
例如:
hello
成为:
hellO
有点复杂但很有趣:
echo strrev(ucfirst(strrev("hello")));
作为一个函数:
function uclast($str) {
return strrev(ucfirst(strrev($str)));
}
$str = strtolower(the_title());
。如果不行,那么你的 the_title()
函数可能存在错误。 - bob-the-destroyerstrtolower
。谢谢 @bob-the-destroyer。 - karim79get_the_title()
来获取返回值。the_title()
会直接输出。或者使用 the_title('','', false);
来获取返回值。 - hakre当$s
是您的字符串时(演示):
$s[-1] = strtoupper($s[-1]);
或者是以函数的形式:
function uclast(string $s): string
{
$s[-1] = strtoupper($s[-1]);
return $s;
}
如果您需要将所有字符转换为小写,除了最后一个字符以外,那么可以使用以下代码:
function uclast(string $s): string
{
if ("" === $s) {
return $s;
}
$s = strtolower($s);
$s[-1] = strtoupper($s[-1]);
return $s;
}
substr()
函数。strtotupper()
。$thestring="Testing testing 3 2 1. aaaa";
echo substr($thestring, 0, strlen($thestring)-2) . strtoupper(substr($thestring, -1));
$string = 'ana-nd';
echo str_replace(substr($string, -3), strtoupper('_'.substr($string, -2)), $string);
// Output: ana_ND
$string = 'anand';
echo str_replace(substr($string, -2), strtoupper(substr($string, -2)), $string);
// Output: anaND
1. Split the string s = xyz where x is the part of
the string before the last letter, y is the last
letter, and z is the part of the string that comes
after the last letter.
2. Compute y = Y, where Y is the upper-case equivalent
of y.
3. Emit S = xYz
以下所有内容均可使用小写/大写/混合字符大小写
<?php
$word = "HELLO";
//or
$word = "hello";
//or
$word = "HeLLo";
$word = strrev(ucfirst(strrev(strtolower($word))));
echo $word;
?>
输出所有单词
hellO
echo strrev(ucfirst(strrev("hello")));
;pecho strrev(ucfirst(strrev("hello")));
;p - karim79