PHP string-end word character enable case-insensitive swaps

Source: Internet
Author: User
This article mainly introduces how to enable case-sensitivity swap for the last character of the PHP string word, which is a very practical technique for PHP string conversion, for more information, see the following example to describe how to enable case-insensitive swaps for the last character of the PHP string word. Share it with you for your reference. The specific implementation method is as follows:

I. requirements:
Returns A string such as "a journey of, a thousand 'Miles 'must can't \" begin \ "with A single step. ", which is changed to" a journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP through the PHP program."

Note:

1. if the last character of each word is in uppercase, it becomes in lowercase. if it is in lowercase, it becomes in uppercase.
2. you need to consider conversion in the form similar to can't.
3. punctuation marks (only consider, '".;) do not change.

II. the reference algorithm is as follows:

The code is as follows:

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

The test code is as follows:

The code is as follows:

<? Php

// Test
$ Str1 = "A journey of, a thousand 'mys' must can't \" begin \ "with a single step .";
$ Str2 = "A journey of, a thousand 'mys' must can't \" begin \ "with a single step .";
$ Str3 = "A journey of, a thousand 'mys' must can't \" begin \ "with a single step. ";
$ 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 'mys' must can't \" begin \ "with a single step. a B \"";

Echo "source:
". $ Str1 ."
Result:
". ConvertLastChar ($ str1 )."

";
Echo "source:
". $ Str2 ."
Result:
". ConvertLastChar ($ str2 )."

";
Echo "source:
". $ Str3 ."
Result:
". ConvertLastChar ($ str3 )."

";
Echo "source:
". $ Str4 ."
Result:
". ConvertLastChar ($ str4 )."

";
Echo "source:
". $ Str5 ."
Result:
". ConvertLastChar ($ str5 )."

";
Echo "source:
". $ Str6 ."
Result:
". ConvertLastChar ($ str6 )."

";
?>

The running result is as follows:

The code is as follows:

Source:
A journey of, a thousand 'mys' must can't "begin" with a single step.
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP.

Source:
A journey of, a thousand 'mys' must can't "begin" with a single step.
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP.

Source:
A journey of, a thousand 'mys' must can't "begin" with a single step.
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP.

Source:
A journey of, a thousand 'mys' must can't "begin" with a single step. a B
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP. A B

Source:
A journey of, a thousand 'mys' must can't "begin" with a single step. a B'
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP. A B'

Source:
A journey of, a thousand 'mys' must can't "begin" with a single step. a B"
Result:
A journeY oF, A thousanD 'mys' musT can't "begiN" witH A singlE steP. A B"

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.