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 在5.1.* 和5.2.*之间 PDO数据库操作中的不同之处小结
Mar 07 PHP
php颜色转换函数hex-rgb(将十六进制格式转成十进制格式)
Sep 23 PHP
PHP实现将科学计数法转换为原始数字字符串的方法
Dec 16 PHP
php中$_POST与php://input的区别实例分析
Jan 07 PHP
php线性表的入栈与出栈实例分析
Jun 12 PHP
Zend Framework实现将session存储在memcache中的方法
Mar 22 PHP
PHP针对字符串开头和结尾的判断方法
Jul 11 PHP
PHP自定义错误用法示例
Sep 28 PHP
微信公众号模板消息群发php代码示例
Dec 29 PHP
php mysql_list_dbs()函数用法示例
Mar 29 PHP
PHP Redis扩展无法加载的问题解决方法
Aug 22 PHP
php源码的使用方法讲解
Sep 26 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
Windows下的PHP安装文件线程安全和非线程安全的区别
2014/04/23 PHP
自定义session存储机制避免会话保持问题
2014/10/08 PHP
php获取url参数方法总结
2014/11/13 PHP
PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例
2017/08/10 PHP
php curl操作API接口类完整示例
2019/05/21 PHP
你所要知道JS(DHTML)中的一些技巧
2007/01/09 Javascript
基于jquery跨浏览器显示的file上传控件
2011/10/24 Javascript
js操作label给label赋值及取label的值示例
2013/11/07 Javascript
jsMind通过鼠标拖拽的方式调整节点位置
2015/04/13 Javascript
JavaScript获取一个范围内日期的方法
2015/04/24 Javascript
javascript中使用正则表达式清理table样式的代码
2020/04/01 Javascript
详细解读JavaScript编程中的Promise使用
2015/07/27 Javascript
动态的9*9乘法表效果的实现代码
2016/05/16 Javascript
Vuejs中使用markdown服务器端渲染的示例
2017/11/22 Javascript
微信小程序中吸底按钮适配iPhone X方案
2017/11/29 Javascript
vue实现学生录入系统之添加删除功能
2018/07/11 Javascript
微信小程序云开发之新手环境配置
2019/05/16 Javascript
vue自定义指令限制输入框输入值的步骤与完整代码
2020/08/30 Javascript
微信小程序实现底部弹出框
2020/11/18 Javascript
[01:19:11]Ti4 循环赛第二日 NaVi.us vs iG
2014/07/11 DOTA
对python中的xlsxwriter库简单分析
2018/05/04 Python
让代码变得更易维护的7个Python库
2018/10/09 Python
对Python协程之异步同步的区别详解
2019/02/19 Python
Python实现一个带权无回置随机抽选函数的方法
2019/07/24 Python
Python字符串中删除特定字符的方法
2020/01/15 Python
python使用Word2Vec进行情感分析解析
2020/07/31 Python
爱尔兰橄榄球店:Irish Rugby Store
2019/12/05 全球购物
甲方资料员岗位职责
2013/12/13 职场文书
高中竞选班长演讲稿
2014/04/24 职场文书
建筑工地门卫岗位职责
2014/04/30 职场文书
软件项目实施计划书
2014/05/02 职场文书
增员口号大全
2014/06/18 职场文书
二审答辩状格式
2015/05/22 职场文书
python字典的元素访问实例详解
2021/07/21 Python
alibaba seata服务端具体实现
2022/02/24 Java/Android
Python下载商品数据并连接数据库且保存数据
2022/03/31 Python