PHP字符串word末字符大小写互换

前端之家收集整理的这篇文章主要介绍了PHP字符串word末字符大小写互换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<h1 class="lead">要求

给出一个字符串如 “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
</span><span style="color: #0000ff;"&gt;function</span> convertLastChar(<span style="color: #800080;"&gt;$str</span><span style="color: #000000;"&gt;) {
    </span><span style="color: #800080;"&gt;$markArr</span> = <span style="color: #0000ff;"&gt;array</span>(",","' ","\" ",". ","; "<span style="color: #000000;"&gt;);
    </span><span style="color: #800080;"&gt;$ret</span> = ""<span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #800080;"&gt;$i</span> = 0,<span style="color: #800080;"&gt;$j</span> = <span style="color: #008080;"&gt;strlen</span>(<span style="color: #800080;"&gt;$str</span>); <span style="color: #800080;"&gt;$i</span> < <span style="color: #800080;"&gt;$j</span>; <span style="color: #800080;"&gt;$i</span>++<span style="color: #000000;"&gt;) {
        </span><span style="color: #0000ff;"&gt;if</span> (<span style="color: #800080;"&gt;$i</span> < <span style="color: #800080;"&gt;$j</span> - 2<span style="color: #000000;"&gt;) {
            </span><span style="color: #800080;"&gt;$afterStr</span> = <span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span> + 1} . <span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span> + 2<span style="color: #000000;"&gt;};
        } </span><span style="color: #0000ff;"&gt;else</span> <span style="color: #0000ff;"&gt;if</span> (<span style="color: #800080;"&gt;$i</span> < <span style="color: #800080;"&gt;$j</span> - 1<span style="color: #000000;"&gt;) {
            </span><span style="color: #800080;"&gt;$afterStr</span> = <span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span> + 1} . " "<span style="color: #000000;"&gt;;
        }
        </span><span style="color: #0000ff;"&gt;if</span> (<span style="color: #008080;"&gt;in_array</span>(<span style="color: #800080;"&gt;$afterStr</span>,<span style="color: #800080;"&gt;$markArr</span><span style="color: #000000;"&gt;) 
            </span>|| <span style="color: #800080;"&gt;$i</span> == <span style="color: #800080;"&gt;$j</span> - 1 
            || <span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span> + 1} == " "<span style="color: #000000;"&gt;) {
            </span><span style="color: #800080;"&gt;$ret</span> .= <span style="color: #008080;"&gt;strtoupper</span>(<span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span>}) === <span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span><span style="color: #000000;"&gt;} 
                </span>? <span style="color: #008080;"&gt;strtolower</span>(<span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span><span style="color: #000000;"&gt;}) 
                </span>: <span style="color: #008080;"&gt;strtoupper</span>(<span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span><span style="color: #000000;"&gt;});
        } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {
            </span><span style="color: #800080;"&gt;$ret</span> .= <span style="color: #800080;"&gt;$str</span>{<span style="color: #800080;"&gt;$i</span><span style="color: #000000;"&gt;};
        }
    }
    </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800080;"&gt;$ret</span><span style="color: #000000;"&gt;;
}

?>

测试

PHP
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;test</span>
<span style="color: #800080;"&gt;$str1</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step."<span style="color: #000000;"&gt;;
</span><span style="color: #800080;"&gt;$str2</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. "<span style="color: #000000;"&gt;;
</span><span style="color: #800080;"&gt;$str3</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a "<span style="color: #000000;"&gt;;
</span><span style="color: #800080;"&gt;$str4</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a B"<span style="color: #000000;"&gt;;
</span><span style="color: #800080;"&gt;$str5</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a b'"<span style="color: #000000;"&gt;;
</span><span style="color: #800080;"&gt;$str6</span> = "A journey of,a thousand 'miles' must can't \"begin\" with a single step. a B\""<span style="color: #000000;"&gt;;

</span><span style="color: #0000ff;"&gt;echo</span> "source:<br/>" . <span style="color: #800080;"&gt;$str1</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;"&gt;$str1</span>) . "<br/><br/>"<span style="color: #000000;"&gt;;
</span><span style="color: #0000ff;"&gt;echo</span> "source:<br/>" . <span style="color: #800080;"&gt;$str2</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;"&gt;$str2</span>) . "<br/><br/>"<span style="color: #000000;"&gt;;
</span><span style="color: #0000ff;"&gt;echo</span> "source:<br/>" . <span style="color: #800080;"&gt;$str3</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;"&gt;$str3</span>) . "<br/><br/>"<span style="color: #000000;"&gt;;
</span><span style="color: #0000ff;"&gt;echo</span> "source:<br/>" . <span style="color: #800080;"&gt;$str4</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;"&gt;$str4</span>) . "<br/><br/>"<span style="color: #000000;"&gt;;
</span><span style="color: #0000ff;"&gt;echo</span> "source:<br/>" . <span style="color: #800080;"&gt;$str5</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;"&gt;$str5</span>) . "<br/><br/>"<span style="color: #000000;"&gt;;
</span><span style="color: #0000ff;"&gt;echo</span> "source:<br/>" . <span style="color: #800080;"&gt;$str6</span> . "<br/>result:<br/>" . convertLastChar(<span style="color: #800080;"&gt;$str6</span>) . "<br/><br/>"<span style="color: #000000;"&gt;;

?>

结果:

source:,a thousand 'miles' must can''mileS' musT can'T "begiN" witH A singlE steP.source:<span style="color: #000000;">
A journey of
,a thousand 'miles' must can'<span style="color: #000000;">t "begin" with a single step.
result:
a journeY oF,A thousanD
'mileS' musT can'T "begiN" witH A singlE steP.<span style="color: #000000;">

source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can'<span style="color: #000000;">t "begin" with a single step. a
result:
a journeY oF,A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.<span style="color: #000000;"> A

source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can'<span style="color: #000000;">t "begin" with a single step. a B
result:
a journeY oF,A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.<span style="color: #000000;"> A b

source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can't "begin" with a single step. a b'<span style="color: #000000;">
result:<span style="color: #000000;">
a journeY oF,A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A B'<span style="color: #000000;">

source:<span style="color: #000000;">
A journey of,a thousand 'miles' must can'<span style="color: #000000;">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"<span style="color: #000000;">

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

题目来源

,有改动(增加了can't这种),按改后的规则,原文答案全是错的。

猜你在找的PHP相关文章