文字列の最後の文字だけを大文字にする方法を教えてください。
例えば:
hello
になります:
hellO
複雑だが楽しい:
echo strrev(ucfirst(strrev("hello")));
関数として:
function uclast($str) {
return strrev(ucfirst(strrev($str)));
}
$s
あなたの文字列はいつですか( Demo):
$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;
}
これには 2 つの部分があります。まず、文字列の一部を取得する方法を知る必要があります。そのためには、関数が必要ですsubstr()
。
次に、 という文字列を大文字にする関数がありますstrtotupper()
。
$thestring="Testing testing 3 2 1. aaaa";
echo substr($thestring, 0, strlen($thestring)-2) . strtoupper(substr($thestring, -1));
以下のすべてに小文字/大文字/混合文字ケースを使用できます
<?php
$word = "HELLO";
//or
$word = "hello";
//or
$word = "HeLLo";
$word = strrev(ucfirst(strrev(strtolower($word))));
echo $word;
?>
すべての単語の出力
hellO
Here's an algorithm:
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
$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