Home >Backend Development >PHP Tutorial >How to exchange case between characters at the end of word in PHP string, string case_PHP tutorial

How to exchange case between characters at the end of word in PHP string, string case_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:28885browse

How to implement case swapping of characters at the end of word in PHP string, string case

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:

Copy code The code is as follows:
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 code The 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.
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程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/909347.htmlTechArticlePHP字符串word末字符实现大小写互换的方法,字符串大小写 本文实例讲述了PHP字符串word末字符实现大小写互换的方法。分享给大家供大家参...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn