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 相关文章推荐
使用 php4 加速 web 传输
Oct 09 PHP
php自定义函数之递归删除文件及目录
Aug 08 PHP
Array of country list in PHP with Zend Framework
Oct 17 PHP
浅析关于PHP位运算的简单权限设计
Jun 30 PHP
php自定义的格式化时间示例代码
Dec 05 PHP
php jquery 多文件上传简单实例
Dec 23 PHP
php实现的九九乘法口诀表简洁版
Jul 28 PHP
使用PHP similar text计算两个字符串相似度
Nov 06 PHP
thinkPHP下ueditor的使用方法详解
Dec 26 PHP
php分页原理 分页代码 分页类制作教程
Sep 23 PHP
php自定义函数br2nl实现将html中br换行符转换为文本输入中换行符的方法【与函数nl2br功能相反】
Feb 17 PHP
PHP的mysqli_ssl_set()函数讲解
Jan 23 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中通过数组进行高效随机抽取指定条记录的算法
2013/09/09 PHP
单点登录 Ucenter示例分析
2013/10/29 PHP
ThinkPHP3.1新特性之对分组支持的改进与完善概述
2014/06/19 PHP
[原创]PHP简单开启curl的方法(测试可行)
2016/01/11 PHP
PHP简单获取多个checkbox值的方法
2016/06/13 PHP
JS中简单的实现像C#中using功能(有源码下载)
2007/01/09 Javascript
javascript静态的url如何传递
2007/05/03 Javascript
prototype Element学习笔记(篇一)
2008/10/26 Javascript
JS 控制非法字符的输入代码
2009/12/04 Javascript
JS 表单验证大全
2011/11/23 Javascript
Javascript中封装window.open解决不兼容问题
2014/09/28 Javascript
JS实现仿京东淘宝竖排二级导航
2014/12/08 Javascript
jquery中filter方法用法实例分析
2015/02/06 Javascript
javascript实现uploadify上传格式以及个数限制
2015/11/23 Javascript
AngularJS中scope的绑定策略实例分析
2017/10/30 Javascript
使用async-validator编写Form组件的方法
2018/01/10 Javascript
vue如何进行动画的封装
2018/09/26 Javascript
浅谈Angular7 项目开发总结
2018/12/19 Javascript
Weex开发之地图篇的具体使用
2019/10/16 Javascript
jquery实现弹窗(系统提示框)效果
2019/12/10 jQuery
json.stringify()与json.parse()的区别以及用处
2021/01/25 Javascript
Python cookbook(字符串与文本)在字符串的开头或结尾处进行文本匹配操作
2018/04/20 Python
python 实现判断ip连通性的方法总结
2018/04/22 Python
利用Python如何实现一个小说网站雏形
2018/11/23 Python
python模拟点击网页按钮实现方法
2020/02/25 Python
OpenCV读取与写入图片的实现
2020/10/13 Python
python time()的实例用法
2020/11/03 Python
python request 模块详细介绍
2020/11/10 Python
使用HTML5加载音频和视频的实现代码
2020/11/30 HTML / CSS
德国骆驼商店:ActiveFashionWorld
2017/11/18 全球购物
物流管理应届生求职信
2013/11/07 职场文书
教师旷工检讨书
2014/01/18 职场文书
安全生产先进个人材料
2014/02/06 职场文书
小学生元旦感言
2014/02/26 职场文书
党的群众路线教育实践活动个人整改落实情况汇报
2014/10/28 职场文书
社区服务活动报告
2015/02/05 职场文书