PHP字符串word末字符實(shí)現(xiàn)大小寫(xiě)互換的方法
本文實(shí)例講述了PHP字符串word末字符實(shí)現(xiàn)大小寫(xiě)互換的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
一、要求:
給出一個(gè)字符串如 “A journey of, a thousand 'miles' must can't \"begin\" with a single step.” ,通過(guò) PHP 程序處理變成 “a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.”
這里需要注意:
1、每個(gè)單詞最后的字符如果是大寫(xiě)就變成小寫(xiě),如果是小寫(xiě)就變成大寫(xiě)。
2、需要考慮類似 can't 這種形式的轉(zhuǎn)換。
3、標(biāo)點(diǎn)符號(hào)(只考慮 , ' " . ;)不用變化。
二、參考算法如下:
function convertLastChar($str) {
$markArr = array(", ", "' ", "\" ", ". ", "; ");
$ret = "";
for ($i = 0, $j = strlen($str); $i < $j; $i++) {
if ($i < $j - 2) {
$afterStr = $str{$i + 1} . $str{$i + 2};
} else if ($i < $j - 1) {
$afterStr = $str{$i + 1} . " ";
}
if (in_array($afterStr, $markArr)
|| $i == $j - 1
|| $str{$i + 1} == " ") {
$ret .= strtoupper($str{$i}) === $str{$i}
? strtolower($str{$i})
: strtoupper($str{$i});
} else {
$ret .= $str{$i};
}
}
return $ret;
}
?>
測(cè)試代碼如下:
//test
$str1 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step.";
$str2 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. ";
$str3 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a ";
$str4 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B";
$str5 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a b'";
$str6 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B\"";
echo "source:<br/>" . $str1 . "<br/>result:<br/>" . convertLastChar($str1) . "<br/><br/>";
echo "source:<br/>" . $str2 . "<br/>result:<br/>" . convertLastChar($str2) . "<br/><br/>";
echo "source:<br/>" . $str3 . "<br/>result:<br/>" . convertLastChar($str3) . "<br/><br/>";
echo "source:<br/>" . $str4 . "<br/>result:<br/>" . convertLastChar($str4) . "<br/><br/>";
echo "source:<br/>" . $str5 . "<br/>result:<br/>" . convertLastChar($str5) . "<br/><br/>";
echo "source:<br/>" . $str6 . "<br/>result:<br/>" . convertLastChar($str6) . "<br/><br/>";
?>
運(yùn)行結(jié)果如下:
A journey of, a thousand 'miles' must can't "begin" with a single step.
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.
source:
A journey of, a thousand 'miles' must can't "begin" with a single step.
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a B
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A b
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a b'
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A B'
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a B"
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A b"
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- PHP隨機(jī)字符串生成代碼(包括大小寫(xiě)字母)
- PHP stristr() 函數(shù)(不區(qū)分大小寫(xiě)的字符串查找)
- PHP將字符串首字母大小寫(xiě)轉(zhuǎn)換的實(shí)例
- PHP英文字母大小寫(xiě)轉(zhuǎn)換函數(shù)小結(jié)
- php大小寫(xiě)轉(zhuǎn)換函數(shù)(strtolower、strtoupper)用法介紹
- php根據(jù)操作系統(tǒng)轉(zhuǎn)換文件名大小寫(xiě)的方法
- PHP 實(shí)現(xiàn)人民幣小寫(xiě)轉(zhuǎn)換成大寫(xiě)的方法及大小寫(xiě)轉(zhuǎn)換函數(shù)
- PHP實(shí)現(xiàn)字符串大小寫(xiě)轉(zhuǎn)函數(shù)的功能實(shí)例
相關(guān)文章
解析dedeCMS驗(yàn)證碼的實(shí)現(xiàn)代碼
本篇文章是對(duì)dedeCMS驗(yàn)證碼的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06關(guān)于php程序報(bào)date()警告的處理(date_default_timezone_set)
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function2013-10-10php常用字符串查找函數(shù)strstr()與strpos()實(shí)例分析
這篇文章主要介紹了php常用字符串查找函數(shù)strstr()與strpos(),結(jié)合具體實(shí)例形式分析了php字符串查找函數(shù)strstr()與strpos()的具體功能、用法、區(qū)別及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-06-06