PHP字符串word末字符大小写互换,字符串大小写_PHP教程

WBOY
풀어 주다: 2016-07-13 10:14:35
원래의
797명이 탐색했습니다.

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

参考算法

<?<span>php

    </span><span>function</span> convertLastChar(<span>$str</span><span>) {
        </span><span>$markArr</span> = <span>array</span>(", ", "' ", "\" ", ". ", "; "<span>);
        </span><span>$ret</span> = ""<span>;
        </span><span>for</span> (<span>$i</span> = 0, <span>$j</span> = <span>strlen</span>(<span>$str</span>); <span>$i</span> < <span>$j</span>; <span>$i</span>++<span>) {
            </span><span>if</span> (<span>$i</span> < <span>$j</span> - 2<span>) {
                </span><span>$afterStr</span> = <span>$str</span>{<span>$i</span> + 1} . <span>$str</span>{<span>$i</span> + 2<span>};
            } </span><span>else</span> <span>if</span> (<span>$i</span> < <span>$j</span> - 1<span>) {
                </span><span>$afterStr</span> = <span>$str</span>{<span>$i</span> + 1} . " "<span>;
            }
            </span><span>if</span> (<span>in_array</span>(<span>$afterStr</span>, <span>$markArr</span><span>) 
                </span>|| <span>$i</span> == <span>$j</span> - 1 
                || <span>$str</span>{<span>$i</span> + 1} == " "<span>) {
                </span><span>$ret</span> .= <span>strtoupper</span>(<span>$str</span>{<span>$i</span>}) === <span>$str</span>{<span>$i</span><span>} 
                    </span>? <span>strtolower</span>(<span>$str</span>{<span>$i</span><span>}) 
                    </span>: <span>strtoupper</span>(<span>$str</span>{<span>$i</span><span>});
            } </span><span>else</span><span> {
                </span><span>$ret</span> .= <span>$str</span>{<span>$i</span><span>};
            }
        }
        </span><span>return</span> <span>$ret</span><span>;
    }

</span>?>
로그인 후 복사

测试

<?<span>php

    </span><span>//</span><span>test</span>
    <span>$str1</span> = "A journey of, a thousand 'miles' must can't \"begin\" with a single step."<span>;
    </span><span>$str2</span> = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. "<span>;
    </span><span>$str3</span> = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a "<span>;
    </span><span>$str4</span> = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B"<span>;
    </span><span>$str5</span> = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a b'"<span>;
    </span><span>$str6</span> = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a B\""<span>;

    </span><span>echo</span> "source:<br/>" . <span>$str1</span> . "<br/>result:<br/>" . convertLastChar(<span>$str1</span>) . "<br/><br/>"<span>;
    </span><span>echo</span> "source:<br/>" . <span>$str2</span> . "<br/>result:<br/>" . convertLastChar(<span>$str2</span>) . "<br/><br/>"<span>;
    </span><span>echo</span> "source:<br/>" . <span>$str3</span> . "<br/>result:<br/>" . convertLastChar(<span>$str3</span>) . "<br/><br/>"<span>;
    </span><span>echo</span> "source:<br/>" . <span>$str4</span> . "<br/>result:<br/>" . convertLastChar(<span>$str4</span>) . "<br/><br/>"<span>;
    </span><span>echo</span> "source:<br/>" . <span>$str5</span> . "<br/>result:<br/>" . convertLastChar(<span>$str5</span>) . "<br/><br/>"<span>;
    </span><span>echo</span> "source:<br/>" . <span>$str6</span> . "<br/>result:<br/>" . convertLastChar(<span>$str6</span>) . "<br/><br/>"<span>;
</span>?>
로그인 후 복사

结果:

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

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

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

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

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

source</span>:<span>
A journey of</span>, a thousand 'miles' must can'<span>t "begin" with a single step. a B"
result:
a journeY oF, A thousanD </span>'mileS' musT can'T "begiN" witH A singlE steP. A b"<span><br /></span>
로그인 후 복사

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

题目来源

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/908456.htmlTechArticlePHP字符串word末字符大小写互换,字符串大小写 要求 给出一个字符串如 A journey of, a thousand 'miles' must can't \"begin\" with a single step. ,通过 PH...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿