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、标点符号(只考虑 , ‘ " . ;)不用变化。

参考算法

<?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;
    }

?>

测试

<?php

    //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:<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/>";
?>

结果:

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"

我们可以看到,是符合预期的。

题目来源

http://blog.sijiaomao.com/?p=98,有改动(增加了can‘t这种),按改后的规则,原文答案全是错的。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。