PHP字符串word末字符实现大小写互换的方法


Posted in PHP onNovember 10, 2014

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

二、参考算法如下:

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

    }

?>

测试代码如下:

<?php
    //test

    $str1 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step.";

    $str2 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. ";

    $str3 = "A journey of, a thousand 'miles' must can't \"begin\" with a single step. a ";

    $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 'miles' must can't \"begin\" with a single step. a B\"";
    echo "source:<br/>" . $str1 . "<br/>result:<br/>" . convertLastChar($str1) . "<br/><br/>";

    echo "source:<br/>" . $str2 . "<br/>result:<br/>" . convertLastChar($str2) . "<br/><br/>";

    echo "source:<br/>" . $str3 . "<br/>result:<br/>" . convertLastChar($str3) . "<br/><br/>";

    echo "source:<br/>" . $str4 . "<br/>result:<br/>" . convertLastChar($str4) . "<br/><br/>";

    echo "source:<br/>" . $str5 . "<br/>result:<br/>" . convertLastChar($str5) . "<br/><br/>";

    echo "source:<br/>" . $str6 . "<br/>result:<br/>" . convertLastChar($str6) . "<br/><br/>";

?>

运行结果如下:

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"

希望本文所述对大家的PHP程序设计有所帮助。

PHP 相关文章推荐
用Socket发送电子邮件
Oct 09 PHP
我的论坛源代码(一)
Oct 09 PHP
Sorting Array Values in PHP(数组排序)
Sep 15 PHP
Zend的Registry机制的使用说明
May 02 PHP
有关于PHP中常见数据类型的汇总分享
Jan 06 PHP
php实现的美国50个州选择列表实例
Apr 20 PHP
php使用函数pathinfo()、parse_url()和basename()解析URL
Nov 25 PHP
Yii全局函数用法示例
Jan 22 PHP
yii2使用GridView实现数据全选及批量删除按钮示例
Mar 01 PHP
PHP实现蛇形矩阵,回环矩阵及数字螺旋矩阵的方法分析
May 29 PHP
laravel 实现根据字段不同值做不同查询
Oct 23 PHP
使用Git实现Laravel项目的自动化部署
Nov 24 PHP
PHP 快速排序算法详解
Nov 10 #PHP
PHP基于CURL进行POST数据上传实例
Nov 10 #PHP
ci检测是ajax还是页面post提交数据的方法
Nov 10 #PHP
php采用ajax数据提交post与post常见方法总结
Nov 10 #PHP
php学习笔记之面向对象
Nov 08 #PHP
php学习笔记之基础知识
Nov 08 #PHP
推荐一款MAC OS X 下php集成开发环境mamp
Nov 08 #PHP
You might like
PHP编写的图片验证码类文件分享
2016/06/06 PHP
jquery+thinkphp实现跨域抓取数据的方法
2016/10/15 PHP
利用PHPExcel实现Excel文件的写入和读取
2017/04/26 PHP
延时重复执行函数 lLoopRun.js
2007/05/08 Javascript
javascript 写类方式之七
2009/07/05 Javascript
基于jQuery判断两个元素是否有重叠部分的代码
2012/07/25 Javascript
javascript实现颜色渐变的方法
2013/10/30 Javascript
js中settimeout方法加参数的使用实例
2014/02/27 Javascript
JS自定义对象实现Java中Map对象功能的方法
2015/01/20 Javascript
jQuery实现跨域
2015/02/03 Javascript
Javascript无参数和有参数类继承问题解决方法
2015/03/02 Javascript
JavaScrip常见的一些算法总结
2015/12/28 Javascript
AngularJS 最常用的功能汇总
2016/02/17 Javascript
总结JavaScript设计模式编程中的享元模式使用
2016/05/21 Javascript
JavaScript SHA-256加密算法详细代码
2016/10/06 Javascript
手机端实现Bootstrap简单图片轮播效果
2016/10/13 Javascript
Bootstrap进度条与AJAX后端数据传递结合使用实例详解
2017/04/23 Javascript
javascript 玩转Date对象(实例讲解)
2017/07/11 Javascript
微信小程序三级联动地址选择器的实例代码
2017/07/12 Javascript
ES6使用Set数据结构实现数组的交集、并集、差集功能示例
2017/10/31 Javascript
Express下采用bcryptjs进行密码加密的方法
2018/02/07 Javascript
vue2.0 循环遍历加载不同图片的方法
2018/03/06 Javascript
详解element-ui中表单验证的三种方式
2019/09/18 Javascript
用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化
2015/04/01 Python
Python subprocess库的使用详解
2018/10/26 Python
对python中Json与object转化的方法详解
2018/12/31 Python
Python实现决策树并且使用Graphviz可视化的例子
2019/08/09 Python
澳大利亚在线百货商店:Real Smart
2017/08/13 全球购物
经典c++面试题四
2015/05/14 面试题
师范教师大学生职业生涯规划范文
2014/01/05 职场文书
火灾现场处置方案
2014/05/28 职场文书
学校工作推荐信范文
2014/07/11 职场文书
营销与策划实训报告
2014/11/05 职场文书
心理健康教育培训研修感言
2015/11/18 职场文书
幼儿园托班开学寄语(2016春季)
2015/12/03 职场文书
2016年大学生党员公开承诺书
2016/03/24 职场文书