2011-07-26 42 views
5

はどのように大文字にすることができますか?文字列の最後の文字。例えば文字列の最後の文字を大文字にする

hello 

は次のようになります。これには2つの部分があります

hellO 
+0

ため

<?php $word = "HELLO"; //or $word = "hello"; //or $word = "HeLLo"; $word = strrev(ucfirst(strrev(strtolower($word)))); echo $word; ?> 

出力を使用することができますか?あなたは段落/文/単語を意味しますか?さもなければ、あなたが制御する文字列変数の最後の位置で 'strtoupper()'を実行しているだけです。 –

+4

'echo strrev(ucfirst(strrev(" hello ")));'; – karim79

+0

@ karim79これは私のアイデアよりはるかに優れています。 – Brad

答えて

1

。まず、文字列の一部を取得する方法を知る必要があります。そのためには、substr()機能が必要です。

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

$thestring="Testing testing 3 2 1. aaaa"; 
echo substr($thestring, 0, strlen($thestring)-2) . strtoupper(substr($thestring, -1)); 
12

回旋けど楽しい:

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

デモ:関数としてhttp://ideone.com/7QK5B

function uclast($str) { 
    return strrev(ucfirst(strrev($str))); 
} 
+0

それは素晴らしい作品です!私がそれをちょうど私のように入力するとき、私がしようとしているのはWordPressでページタイトルをすべて小文字に変更し、最後の文字を大文字に変更することです。たとえば、タイトルはSample Pageとして表示されます。私はそれをサンプルページとして表示します。私が試したことをheresしかし、 'コード'は動作していない<?php \t \t \t \t \t $ str = the_title(); \t \t \t \t \t echo strrev(ucfirst(strrev($ str))); \t \t \t \t \t?> – Kathy

+0

ここでコードブロックを行う方法がわかりません:) – Kathy

+1

@Kathy: '$ str = strtolower(the_title());'動作しない場合、 'the_title()'関数にエラーがある可能性があります。 –

0

ここアルゴリズムです:

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 
2

$sはあなたの文字列(Demo)です:

$s[$l=strlen($s)-1] = strtoupper($s[$l]); 

または関数の形で:

function uclast($s) 
{ 
    $l=strlen($s)-1; 
    $s[$l] = strtoupper($s[$l]); 
    return $s; 
} 

そして、あなたの拡張ニーズが最後の文字明示的に上位除くすべて小文字を持っているため - ケース:

function uclast($s) 
{ 
    $l=strlen($s)-1; 
    $s = strtolower($s); 
    $s[$l] = strtoupper($s[$l]); 
    return $s; 
} 
0

続きのすべての文字の小文字/大文字/大文字/小文字すべての単語列の

hellO 
関連する問題