PHP string Word end character Case interchange

Source: Internet
Author: User

Requirements

Given a string such as "A journey of, a thousand ' miles ' must can ' t \" begin\ "with a single step." By PHP program processing becomes "A journey of, a thous and ' MileS ' must can ' T ' BegiN ' with A single SteP. "

Attention:
1. If the last character of each word is uppercase, it becomes lowercase, and if lowercase, it becomes uppercase.
2. It is necessary to consider the conversion of this form like can ' t.
3, punctuation (only consider, ' ".; ) without change.

Reference algorithms
<?PHPfunctionConvertlastchar ($str) {        $MARKARR=Array(", ", "‘ ", "\" ", ". ", "; "); $ret= "";  for($i= 0,$j=strlen($str);$i<$j;$i++) {            if($i<$j+ t) {                $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; }?>
Test
<?PHP//Test    $str 1= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step."; $str 2= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. "; $STR 3= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A; $STR 4= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A B "; $STR 5= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A B ' "; $str 6= "A journey of, a thousand ' miles ' must can ' t \" begin\ "with A single step. A b\ ""; Echo"Source:<br/>".$str 1. "<br/>result:<br/>". Convertlastchar ($str 1) . "<br/><br/>"; Echo"Source:<br/>".$str 2. "<br/>result:<br/>". Convertlastchar ($str 2) . "<br/><br/>"; Echo"Source:<br/>".$STR 3. "<br/>result:<br/>". Convertlastchar ($STR 3) . "<br/><br/>"; Echo"Source:<br/>".$STR 4. "<br/>result:<br/>". Convertlastchar ($STR 4) . "<br/><br/>"; Echo"Source:<br/>".$STR 5. "<br/>result:<br/>". Convertlastchar ($STR 5) . "<br/><br/>"; Echo"Source:<br/>".$str 6. "<br/>result:<br/>". Convertlastchar ($str 6) . "<br/><br/>";?>

Results:

SourceA Journey of, a thousand ' miles ' must can 'T "Begin" with a single step.result:a JourneY of, a thousand' MileS ' must can ' T ' BegiN ' with A single SteP.Source:A Journey of, a thousand ' miles ' must can 'T "Begin" with a single step. Result:a JourneY of, a thousand' MileS ' must can ' T ' BegiN ' with A single SteP.Source:A Journey of, a thousand ' miles ' must can 'T "Begin" with a single step. A result:a JourneY of, a thousand' MileS ' must can ' T ' BegiN ' with A single SteP.A Source:A Journey of, a thousand ' miles ' must can 'T "Begin" with a single step. A bresult:a JourneY of, a thousand' MileS ' must can ' T ' BegiN ' with A single SteP.A Bsource:A Journey of, a thousand ' miles ' must can ' 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 'Source:A Journey of, a thousand ' miles ' must can '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 "

We can see that it is in line with expectations.

Source of the topic

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

PHP string Word end character Case interchange

Related Article

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.