PHP字符串如何實現word末字符實現大小寫互換
PHP字符串如何實現word末字符實現大小寫互換?希望下面的內容對你有所幫助,更多詳情請關注應屆畢業生考試網。
一、要求:
給出一個字符串如 “A journey of, a thousand 'miles' must can't "begin" with a single step.” ,通過 PHP 程序處理變成 “a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.”
這裏需要注意:
1、每個單詞最後的.字符如果是大寫就變成小寫,如果是小寫就變成大寫。
2、需要考慮類似 can't 這種形式的轉換。
3、標點符號(只考慮 , ' " . ;)不用變化。
二、參考算法如下:
代碼如下:
<?php
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;
}
?>
測試代碼如下:
代碼如下:
<?php
//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/>";
?>
運行結果如下:
代碼如下:
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.
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"
-
PHP 數組和字符串互相轉換實現方法
PHP數組和字符串互相轉換是比較常用的,下面給大家介紹PHP數組和字符串互相轉換實現方法,一起來了解一下吧!PHP數組和字符串互相轉換實現方法PHP中由於數組和字符串這兩種變量類型是如此常用,以至於PHP具有兩個函數,可以在字符串和數組之間互相進行轉換代碼如下:$ar...
-
作為程序員必知的16個最佳PHP庫
PHP是一種功能強大的web站點腳本語言,通過PHP,web網站開發者可以更容易地創建動態的引人入勝的web頁面。開發人員可以使用PHP代碼與一些網站模板和框架來提升功能和特性。然而,編寫PHP代碼是一個繁瑣又耗時的過程。為了縮短開發時間,開發人員可以用PHP庫替代編寫代...
-
PHP與UTF-8之間的最佳實踐
《PHP中的字符串、編碼、UTF-8》一文中描述了一些列的基礎知識,比較枯燥,現在來説點有用的——PHP字符串處理的最佳實踐,本文是PHP、字符串、編碼、UTF-8相關知識的第二部分。先説結論——在PHP中的各個方面使用UTF-8編碼。希望對大家學習PHP...
-
PHP考試筆試題
用寶珠打扮自己,不如用知識充實自己。以下是小編為大家搜索整理的PHP考試筆試題,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!1、{PHP題目}標識符是變量的名稱。PHP中的標識符用$+變量名來表示。標識符在PHP中遵循下列選項中的那些規則?()...