2011-07-26 70 views
5

如何只能字符串的最后一个字母为字符串的最后一个字母的大小写

例如:

hello 

变为:

hellO 
+0

一个字符串?你的意思是一段/句子/单词?否则,你只是在你控制的字符串变量的最后一个位置上执行'strtoupper()'。 –

+4

'echo strrev(ucfirst(strrev(“hello”)))''p – 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。我希望它显示为示例pagE。继承人我试过,但它不工作的代码<?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

小写字母/大写字母/混合字符的情况下的所有内容可用于

<?php 
    $word = "HELLO"; 

    //or 

    $word = "hello"; 

    //or 

    $word = "HeLLo"; 

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

    echo $word; 
?> 

输出的所有单词

hellO 
相关问题