Home > Backend Development > PHP Tutorial > PHP string word last character case exchange, string case_PHP tutorial

PHP string word last character case exchange, string case_PHP tutorial

WBOY
Release: 2016-07-13 10:14:35
Original
862 people have browsed it

PHP string word last character case exchange, string case

requirement

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:
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.

Reference Algorithm

<?<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>?>
Copy after login

Test

<?<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>?>
Copy after login

Result:

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>
Copy after login

We can see that it is in line with expectations.

Source of the question

http://blog.sijiaomao.com/?p=98, there are changes (can't is added), according to the revised rules, all the original answers are wrong.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/908456.htmlTechArticlePHP string word end character case exchange, string case requirements give a string such as A journey of, a thousand 'miles' must can't "begin" with a single step. , via PH...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template