[英]Capitalize last letter of a string
如何只將字符串的最后一個字母大寫。
例如:
hello
變為:
hellO
令人困惑但有趣:
echo strrev(ucfirst(strrev("hello")));
演示: http : //ideone.com/7QK5B
作為一個功能:
function uclast($str) {
return strrev(ucfirst(strrev($str)));
}
當$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;
}
這有兩個部分。 首先,您需要知道如何獲取字符串的一部分。 為此,您需要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
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.