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 jquery 实现新闻标签分类与无刷新分页
Dec 18 PHP
php cookies中删除的一般赋值方法
May 07 PHP
PHP的SQL注入过程分析
Jan 06 PHP
php判断两个浮点数是否相等的方法
Mar 14 PHP
PHP输出九九乘法表代码实例
Mar 27 PHP
简单谈谈PHP中strlen 函数
Feb 27 PHP
php 一维数组的循环遍历实现代码
Apr 10 PHP
PHP函数按引用传递参数及函数可选参数用法示例
Jun 04 PHP
Laravel框架生命周期与原理分析
Jun 12 PHP
关于php开启错误提示的总结
Sep 24 PHP
PHP 对象接口简单实现方法示例
Apr 13 PHP
2020最新版 PhpStudy V8.1版本下载安装使用详解
Oct 30 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 empty()与isset()区别的详细介绍
2013/06/17 PHP
YII中assets的使用示例
2014/07/31 PHP
PHP获取一年中每个星期的开始和结束日期的方法
2015/02/12 PHP
Yii2中使用join、joinwith多表关联查询
2016/06/30 PHP
PHP时间处理类操作示例
2018/09/05 PHP
js一组验证函数
2008/12/20 Javascript
Javascript面向对象编程
2012/03/18 Javascript
深入剖析JavaScript中的枚举功能
2014/03/06 Javascript
jquery分页对象使用示例
2014/04/01 Javascript
原生Javascript封装的一个AJAX函数分享
2014/10/11 Javascript
浅析jquery如何判断滚动条滚到页面底部并执行事件
2016/04/29 Javascript
[原创]Javascript 实现广告后加载 可加载百度谷歌联盟广告
2016/05/11 Javascript
浅谈Javascript数据属性与访问器属性
2016/07/26 Javascript
js仿微信抢红包功能
2020/09/25 Javascript
javascript 判断用户有没有操作页面
2017/10/17 Javascript
vue轮播图插件vue-concise-slider的使用
2018/03/13 Javascript
vue使用ajax获取后台数据进行显示的示例
2018/08/09 Javascript
Vue.directive 实现元素scroll逻辑复用
2019/11/29 Javascript
python将人民币转换大写的脚本代码
2013/02/10 Python
python3新特性函数注释Function Annotations用法分析
2016/07/28 Python
Python基于回溯法子集树模板实现8皇后问题
2017/09/01 Python
pandas数据集的端到端处理
2019/02/18 Python
Django 中间键和上下文处理器的使用
2019/03/17 Python
Pandas读写CSV文件的方法示例
2019/03/27 Python
django之静态文件 django 2.0 在网页中显示图片的例子
2019/07/28 Python
Python 3.6打包成EXE可执行程序的实现
2019/10/18 Python
Python Selenium操作Cookie的实例方法
2021/02/28 Python
使用CSS3中的calc()属性来以算式表达尺寸数值
2016/06/06 HTML / CSS
CSS3 实现童年的纸飞机
2019/05/05 HTML / CSS
台湾乐天市场:日本No.1的网路购物网站
2017/03/22 全球购物
越南综合购物网站:Lazada越南
2019/06/10 全球购物
超级英雄、电影和电视、乐队和音乐T恤:Loud Clothing
2019/09/01 全球购物
Goodee官方商店:迷你投影仪
2021/03/15 全球购物
2015年社区文体活动总结
2015/03/25 职场文书
医院合作意向书范本
2015/05/08 职场文书
MyBatis自定义SQL拦截器示例详解
2021/10/24 Java/Android