6

文字列の最後の文字だけを大文字にする方法を教えてください。

例えば:

hello

になります:

hellO
4

6 に答える 6

14

複雑だが楽しい:

echo strrev(ucfirst(strrev("hello")));

デモ: http://ideone.com/7QK5B

関数として:

function uclast($str) {
    return strrev(ucfirst(strrev($str)));
}
于 2011-07-26T00:34:20.717 に答える
3

$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;
}
于 2011-07-26T00:59:39.107 に答える
1

これには 2 つの部分があります。まず、文字列の一部を取得する方法を知る必要があります。そのためには、関数が必要ですsubstr()

次に、 という文字列を大文字にする関数がありますstrtotupper()

$thestring="Testing testing 3 2 1. aaaa";
echo substr($thestring, 0, strlen($thestring)-2) . strtoupper(substr($thestring, -1));
于 2011-07-26T00:33:46.487 に答える
0

以下のすべてに小文字/大文字/混合文字ケースを使用できます

<?php
    $word = "HELLO";

    //or

    $word = "hello";

    //or

    $word = "HeLLo";

    $word = strrev(ucfirst(strrev(strtolower($word))));

    echo $word;
?>

すべての単語の出力

hellO
于 2013-10-08T12:02:49.630 に答える
0

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
于 2011-07-26T00:35:44.413 に答える
0
$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
于 2019-05-15T03:51:54.767 に答える