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 相关文章推荐
解决163/sohu/sina不能够收到PHP MAIL函数发出邮件的问题
Mar 13 PHP
PHP 将图片按创建时间进行分类存储的实现代码
Jan 05 PHP
php写的带缓存数据功能的mysqli类
Sep 06 PHP
VB中的RasEnumConnections函数返回632错误解决方法
Jul 29 PHP
php实现的农历算法实例
Aug 11 PHP
PHP中__autoload和Smarty冲突的简单解决方法
Apr 08 PHP
PHP获取IP地址所在地信息的实例(使用纯真IP数据库qqwry.dat)
Nov 15 PHP
让ThinkPHP的模板引擎达到最佳效率的方法详解
Mar 14 PHP
PHP实现动态添加XML中数据的方法
Mar 30 PHP
php压缩文件夹最新版
Jul 18 PHP
Laravel框架控制器的middleware中间件用法分析
Sep 30 PHP
简单的php购物车代码
Jun 05 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
基于mysql的论坛(7)
2006/10/09 PHP
基于PHP异步执行的常用方式详解
2013/06/03 PHP
eAccelerator的安装与使用详解
2013/06/13 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
Yii2压缩PHP中模板代码的输出问题
2018/08/28 PHP
js函数的引用, 关于内存的开销
2012/09/17 Javascript
Jquery中给animation加更多的运作效果实例
2013/09/05 Javascript
JS 有趣的eval优化输入验证实例代码
2013/09/22 Javascript
js判断IE浏览器版本过低示例代码
2013/11/22 Javascript
通过pjax实现无刷新翻页(兼容新版jquery)
2014/01/31 Javascript
JS实现点击按钮控制Div变宽、增高及调整背景色的方法
2015/08/05 Javascript
JS实现仿百度文库评分功能
2017/01/12 Javascript
详解jquery插件jquery.viewport.js学习使用方法
2017/09/08 jQuery
JavaScript学习笔记之DOM操作实例分析
2019/01/08 Javascript
JavaScript数据结构与算法之基本排序算法定义与效率比较【冒泡、选择、插入排序】
2019/02/21 Javascript
详解Vue 匿名、具名和作用域插槽的使用方法
2019/04/22 Javascript
js实现无限瀑布流实例方法
2019/09/16 Javascript
python3实现UDP协议的服务器和客户端
2017/06/14 Python
Python从零开始创建区块链
2018/03/06 Python
python遍历文件夹,指定遍历深度与忽略目录的方法
2018/07/11 Python
python 实现目录复制的三种小结
2019/12/04 Python
Python常用库大全及简要说明
2020/01/17 Python
如何基于pythonnet调用halcon脚本
2020/01/20 Python
pycharm不能运行.py文件的解决方法
2020/02/12 Python
python使用docx模块读写docx文件的方法与docx模块常用方法详解
2020/02/17 Python
Python按照list dict key进行排序过程解析
2020/04/04 Python
基于python代码批量处理图片resize
2020/06/04 Python
python pandas dataframe 去重函数的具体使用
2020/07/20 Python
机电专业个人自荐信格式模板
2013/09/23 职场文书
霸王洗发水广告词
2014/03/14 职场文书
预备党员对照检查材料思想汇报
2014/09/24 职场文书
社团个人总结范文
2015/03/05 职场文书
体检通知范文
2015/04/21 职场文书
初中生物教学反思
2016/02/20 职场文书
Mysql基础之常见函数
2021/04/22 MySQL
JavaScript原型链详解
2021/11/07 Javascript