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 字符串替换的方法
Jan 10 PHP
php DOS攻击实现代码(附如何防范)
May 29 PHP
PHP调用Linux的命令行执行文件压缩命令
Jan 27 PHP
用mysql_fetch_array()获取当前行数据的方法详解
Jun 05 PHP
深入PHP获取随机数字和字母的方法详解
Jun 06 PHP
php删除数组元素示例分享
Feb 17 PHP
php生成QRcode实例
Sep 22 PHP
Zend Framework教程之配置文件application.ini解析
Mar 10 PHP
PHP CURL post数据报错 failed creating formpost data
Oct 16 PHP
PHP基于socket实现客户端和服务端通讯功能
Jul 13 PHP
Laravel学习教程之IOC容器的介绍与用例
Aug 15 PHP
浅析PHP反序列化中过滤函数使用不当导致的对象注入问题
Feb 15 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实现的仿阿里巴巴实现同类产品翻页
2009/12/11 PHP
thinkPHP5.0框架简单配置作用域的方法
2017/03/17 PHP
thinkPHP3.2.2框架行为扩展及demo示例
2018/06/19 PHP
PHP将英文数字转换为阿拉伯数字实例讲解
2019/01/28 PHP
JS实现可改变列宽的table实例
2013/07/02 Javascript
javascript页面加载完执行事件代码
2014/02/11 Javascript
javascript事件函数中获得事件源的两种不错方法
2014/03/17 Javascript
javascript教程:关于if简写语句优化的方法
2014/05/17 Javascript
AspNet中使用JQuery boxy插件的确认框
2015/05/20 Javascript
如何解决ligerUI布局时Center中的Tab高度大小
2015/11/24 Javascript
javascript比较语义化版本号的实现代码
2016/09/09 Javascript
微信小程序 条件渲染详解
2016/10/09 Javascript
jQuery将表单序列化成一个Object对象的实例
2016/11/29 Javascript
Popup弹出框添加数据实现方法
2017/10/27 Javascript
vue填坑之webpack run build 静态资源找不到的解决方法
2018/09/03 Javascript
简化版的vue-router实现思路详解
2018/10/19 Javascript
详解vue-router导航守卫
2019/01/19 Javascript
vue动态禁用控件绑定disable的例子
2019/10/28 Javascript
JS+DIV实现拖动效果
2020/02/11 Javascript
[01:08]DOTA2次级职业联赛 - Wings 战队宣传片
2014/12/01 DOTA
Python中的异常处理学习笔记
2015/01/28 Python
PHP实现发送和接收JSON请求
2018/06/07 Python
详解python解压压缩包的五种方法
2019/07/05 Python
在django中自定义字段Field详解
2019/12/03 Python
python实现串口通信的示例代码
2020/02/10 Python
在keras 中获取张量 tensor 的维度大小实例
2020/06/10 Python
Python DataFrame使用drop_duplicates()函数去重(保留重复值,取重复值)
2020/07/20 Python
CSS3盒子模型详解
2013/04/24 HTML / CSS
Jo Malone美国官网:祖玛珑香水
2017/03/27 全球购物
世界上最受欢迎的钓鱼诱饵:Rapala
2019/05/02 全球购物
介绍一下linux的文件权限
2014/07/20 面试题
校园招聘策划书
2014/01/09 职场文书
模特职业生涯规划范文
2014/02/26 职场文书
党员个人对照检查材料范文
2014/09/24 职场文书
学校开除通知书
2015/04/25 职场文书
PyTorch 如何设置随机数种子使结果可复现
2021/05/12 Python