PHP character string end character to enable case-insensitive swaps, string case
This example describes how to enable case-insensitive swaps for the last character of the PHP string word. Share it with you for your reference. The specific implementation method is as follows:
I. Requirements:
Returns A string such as "a journey of, a thousand 'miles 'must can't \" begin \ "with A single step. ", which is changed to" a journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP through the PHP program."
Note:
1. If the last character of each word is in uppercase, it becomes in lowercase. If it is in lowercase, it becomes in uppercase.
2. You need to consider conversion in the form similar to can't.
3. punctuation marks (only consider, '".;) do not change.
II. The reference algorithm is as follows:
Copy codeThe Code is as follows: <? 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;
}
?>
The test code is as follows:
Copy codeThe Code is as follows: <? Php
// Test
$ Str1 = "A journey of, a thousand 'mys' must can't \" begin \ "with a single step .";
$ Str2 = "A journey of, a thousand 'mys' must can't \" begin \ "with a single step .";
$ Str3 = "A journey of, a thousand 'mys' must can't \" begin \ "with a single step. ";
$ 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 'mys' must can't \" begin \ "with a single step. a B \"";
Echo "source: <br/> ". $ str1. "<br/> result: <br/> ". convertLastChar ($ str1 ). "<br/> ";
Echo "source: <br/> ". $ str2. "<br/> result: <br/> ". convertLastChar ($ str2 ). "<br/> ";
Echo "source: <br/> ". $ str3. "<br/> result: <br/> ". convertLastChar ($ str3 ). "<br/> ";
Echo "source: <br/> ". $ str4. "<br/> result: <br/> ". convertLastChar ($ str4 ). "<br/> ";
Echo "source: <br/> ". $ str5. "<br/> result: <br/> ". convertLastChar ($ str5 ). "<br/> ";
Echo "source: <br/> ". $ str6. "<br/> result: <br/> ". convertLastChar ($ str6 ). "<br/> ";
?>
The running result is as follows:
Copy codeThe Code is as follows: source:
A journey of, a thousand 'mys' must can't "begin" with a single step.
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP.
Source:
A journey of, a thousand 'mys' must can't "begin" with a single step.
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP.
Source:
A journey of, a thousand 'mys' must can't "begin" with a single step.
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP.
Source:
A journey of, a thousand 'mys' must can't "begin" with a single step. a B
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP. A B
Source:
A journey of, a thousand 'mys' must can't "begin" with a single step. a B'
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP. A B'
Source:
A journey of, a thousand 'mys' must can't "begin" with a single step. a B"
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP. A B"
I hope this article will help you with PHP programming.