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 相关文章推荐
解析php下载远程图片函数 可伪造来路
Jun 25 PHP
PHP删除HTMl标签的三种解决方法
Jun 30 PHP
PHP中把对象转换为关联数组代码分享
Apr 09 PHP
php使用gzip压缩传输js和css文件的方法
Jul 29 PHP
PHP+Ajax+JS实现多图上传
May 07 PHP
php 防止表单重复提交两种实现方法
Nov 03 PHP
PHP中CheckBox多选框上传失败的代码写法
Feb 13 PHP
php 判断页面或图片是否经过gzip压缩的方法
Apr 05 PHP
PHP基于自定义类随机生成姓名的方法示例
Aug 05 PHP
使用PHP+MySql+Ajax+jQuery实现省市区三级联动功能示例
Sep 15 PHP
php设计模式之适配器模式原理、用法及注意事项详解
Sep 24 PHP
PHP学习记录之常用的魔术常量详解
Dec 12 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另类上传图片的方法(PHP用Socket上传图片)
2013/10/30 PHP
实例简介PHP的一些高级面向对象编程的特性
2015/11/27 PHP
高质量PHP代码的50个实用技巧必备(下)
2016/01/22 PHP
Laravel中Trait的用法实例详解
2016/03/16 PHP
PHP+Apache+Mysql环境搭建教程
2016/08/01 PHP
一个简单的JavaScript数据缓存系统实现代码
2010/10/24 Javascript
High Performance JavaScript(高性能JavaScript)读书笔记分析
2011/05/05 Javascript
疯狂Jquery第一天(Jquery学习笔记)
2012/05/11 Javascript
Mac/Windows下如何安装Node.js
2013/11/22 Javascript
使用iframe window的scroll方法控制iframe页面滚动
2014/03/05 Javascript
弹出窗口并且此窗口带有半透明的遮罩层效果
2014/03/13 Javascript
jQuery对象初始化的传参方式
2015/02/26 Javascript
vue elementui form表单验证的实现
2018/11/11 Javascript
vue 自动化路由实现代码
2019/09/03 Javascript
vue实现登录拦截
2020/06/29 Javascript
Python中的字符串查找操作方法总结
2016/06/27 Python
mac下给python3安装requests库和scrapy库的实例
2018/06/13 Python
Python读取YUV文件,并显示的方法
2018/12/04 Python
详解Python 4.0 预计推出的新功能
2019/07/26 Python
Python数据分析pandas模块用法实例详解
2019/11/20 Python
pytorch GAN伪造手写体mnist数据集方式
2020/01/10 Python
pytorch模型存储的2种实现方法
2020/02/14 Python
python 实现单例模式的5种方法
2020/09/23 Python
Python 数据分析之逐块读取文本的实现
2020/12/14 Python
Python基于mediainfo批量重命名图片文件
2020/12/29 Python
装上这 14 个插件后,PyCharm 真的是无敌的存在
2021/01/11 Python
CSS+jQuery实现的在线答题功能
2015/04/25 HTML / CSS
澳大利亚最受欢迎的女士度假服装:Kabana Shop
2020/10/10 全球购物
高中班长自我鉴定
2013/12/20 职场文书
服装设计专业自荐书范文
2013/12/30 职场文书
自动化职业生涯规划书范文
2014/01/03 职场文书
工作表现自我评价
2014/02/08 职场文书
商业房地产广告语
2014/03/13 职场文书
单位消防安全责任书
2014/07/23 职场文书
大学军训的体会
2014/11/08 职场文书
MySQL查询学习之基础查询操作
2021/05/08 MySQL