PHP string Word end character for case-sensitive method, string case _php Tutorial

Source: Internet
Author: User

PHP string Word end character for case-sensitive method, string case


The example in this article describes how to implement case-by-word characters in a PHP string. Share to everyone for your reference. The implementation method is as follows:

First, 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. "

Here's what to note:

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.

Second, the reference algorithm is as follows:

Copy the Code code 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:
Copy the Code code as follows: <?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:
" . $str 1. "
Result
" . Convertlastchar ($str 1). "

";
echo "Source:
" . $str 2. "
Result
" . Convertlastchar ($str 2). "

";
echo "Source:
" . $str 3. "
Result
" . Convertlastchar ($str 3). "

";
echo "Source:
" . $str 4. "
Result
" . Convertlastchar ($str 4). "

";
echo "Source:
" . $str 5. "
Result
" . Convertlastchar ($str 5). "

";
echo "Source:
" . $str 6. "
Result
" . Convertlastchar ($str 6). "

";
?>

The results of the operation are as follows:
Copy the Code code as follows: 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.
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 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 '

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 "

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/909347.html www.bkjia.com true http://www.bkjia.com/PHPjc/909347.html techarticle PHP string Word end character implementation of the case-based method, the string case of the text of this article describes the PHP string Word to implement the case of the end of the method. Share for everyone to join us ...

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