Home >Backend Development >PHP Tutorial >How to exchange case between characters at the end of word in PHP string, string case_PHP tutorial
The example in this article describes how to implement case swapping of characters at the end of word in a PHP string. Share it with everyone for your reference. The specific implementation method is as follows:
1. Requirements:
Given a string such as "A journey of, a thousand 'miles' must can't "begin" with a single step.", it will be processed by the PHP program into "a journeY oF, A thousanD 'mileS' musT can'T "begiN" with A singlE steP."
Note here:
1. If the last character of each word is uppercase, it will become lowercase. If it is lowercase, it will become uppercase.
2. You need to consider conversions in the form of can't.
3. Punctuation (only , ' " . ;) does not need to be changed.
2. The reference algorithm is as follows:
The test code is as follows:
//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:
" . $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) . "
";
?>
运行结果如下:
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程序设计有所帮助。