加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-25 03:33:30 所属栏目:PHP 来源:互联网
导读:要求给出一个字符串如 “A journey of, a thousand miles must cant begin with a single step.” ,通过 PHP 程序处理变成 “a journeY oF, A thousanD mileS musT canT begiN witH A singlE steP.”注意:1、每个单词最后的字符如果是大写就变成小写,如果

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

参考算法

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

?>

测试

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读