This article mainly introduces how to enable case-sensitivity swap for the last character of the PHP string word, which is a very practical technique for PHP string conversion, for more information, see the following example to describe 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:
The 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:
The 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:
". $ Str1 ."
Result:
". ConvertLastChar ($ str1 )."
";
Echo "source:
". $ Str2 ."
Result:
". ConvertLastChar ($ str2 )."
";
Echo "source:
". $ Str3 ."
Result:
". ConvertLastChar ($ str3 )."
";
Echo "source:
". $ Str4 ."
Result:
". ConvertLastChar ($ str4 )."
";
Echo "source:
". $ Str5 ."
Result:
". ConvertLastChar ($ str5 )."
";
Echo "source:
". $ Str6 ."
Result:
". ConvertLastChar ($ str6 )."
";
?>
The running result is as follows:
The 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.