點擊題目下方兄弟連雲課堂,關注我,我是程序媛,我是IT行業最萌萌噠的訂閱號
這篇文章主要介紹了PHP字符串word末字符實現大小寫互換的方法,是涉及PHP字符串轉換非常實用的技巧,需要的朋友可以參考下
本文實例講述了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、標點符號(只考慮 , ' " . ;)不用變化。
function convertLastChar($str) {$markArr = array(", ", "' ", "\" ", ". ", "; ");for ($i = 0, $j = strlen($str); $i < $j; $i++) {$afterStr = $str{$i + 1} . $str{$i + 2};} else if ($i < $j - 1) {$afterStr = $str{$i + 1} . " ";if (in_array($afterStr, $markArr)|| $str{$i + 1} == " ") {$ret .= strtoupper($str{$i}) === $str{$i}
$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/>";A journey of, a thousand 'miles' must can't "begin" with a single step.a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.A journey of, a thousand 'miles' must can't "begin" with a single step.a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.A journey of, a thousand 'miles' must can't "begin" with a single step. aa journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. AA journey of, a thousand 'miles' must can't "begin" with a single step. a Ba journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A bA journey of, a thousand 'miles' must can't "begin" with a single step. a b'a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A B'A journey of, a thousand 'miles' must can't "begin" with a single step. a B"a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A b"
希望本文所述對大家的PHP程序設計有所幫助。
泥萌好,我是程序媛小編。做過電影、搞過設計、現在在做編程,我想和你們說,這個號我承包了。每天為大家奉上技術文章、行業猛料、職場潛規則.作為一個北漂,程序媛妹子深知咱們幹程序的不易,夜深人靜的夜晚不要一個人哭泣,如果真的想找人傾訴一下,可以加我微信:xdlmeizi,請原諒我一生放蕩不羈求關注,你是我的粉,我是你的人。