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中通过ADO调用Access数据库的方法测试不通过
Dec 31 PHP
PHP数组 为文章加关键字连接 文章内容自动加链接
Dec 29 PHP
php重定向的三种方法分享
Feb 22 PHP
请离开include_once和require_once
Jul 18 PHP
php使用websocket示例详解
Mar 12 PHP
ThinkPHP视图查询详解
Jun 30 PHP
php实现用于删除整个目录的递归函数
Mar 16 PHP
PHP 反射(Reflection)使用实例
May 12 PHP
PHP简单装饰器模式实现与用法示例
Jun 22 PHP
Yii框架中使用PHPExcel的方法分析
Jul 25 PHP
详解Laravel设置多态关系模型别名的方式
Oct 17 PHP
Thinkphp5框架中引入Markdown编辑器操作示例
Jun 03 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的explode和implode的使用说明
2011/07/17 PHP
php中生成随机密码的自定义函数代码
2013/10/21 PHP
PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法
2017/06/20 PHP
php二维码生成以及下载实现
2017/09/28 PHP
laravel 5.5 关闭token的3种实现方式
2019/10/24 PHP
Laravel 微信小程序后端搭建步骤详解
2019/11/26 PHP
指定js可访问其它域名的cookie的方法
2007/09/18 Javascript
ExtJS 简介 让你知道extjs是什么
2008/12/29 Javascript
js解析与序列化json数据(一)json.stringify()的基本用法
2013/02/01 Javascript
JavaScript异步编程:异步数据收集的具体方法
2013/08/19 Javascript
Javascript中的String对象详谈
2014/03/03 Javascript
JavaScript字符串对象replace方法实例(用于字符串替换或正则替换)
2014/10/16 Javascript
轻松创建nodejs服务器(10):处理POST请求
2014/12/18 NodeJs
全面介绍javascript实用技巧及单竖杠
2016/07/18 Javascript
request请求获取参数的实现方法(post和get两种方式)
2016/09/27 Javascript
js实现选项卡内容切换以及折叠和展开效果【推荐】
2017/01/08 Javascript
jQuery实现的两种简单弹窗效果示例
2018/04/18 jQuery
使用ng-packagr打包Angular的方法示例
2018/09/21 Javascript
JS解惑之Object中的key是有序的么
2019/05/06 Javascript
JavaScript实现省市联动效果
2019/11/22 Javascript
Python使用Socket(Https)Post登录百度的实现代码
2012/05/18 Python
Python装饰器的函数式编程详解
2015/02/27 Python
python3实现TCP协议的简单服务器和客户端案例(分享)
2017/06/14 Python
python opencv之SURF算法示例
2018/02/24 Python
对python制作自己的数据集实例讲解
2018/12/12 Python
python ipset管理 增删白名单的方法
2019/01/14 Python
使用python代码进行身份证号校验的实现示例
2019/11/21 Python
无需JS和jQuery代码实现CSS3鼠标浮动放大图片
2016/11/21 HTML / CSS
HTML5中Canvas与SVG的画图原理比较
2013/01/16 HTML / CSS
中专毕业生自我鉴定
2013/11/21 职场文书
运动会通讯稿50字
2014/01/30 职场文书
市场营销工作计划书
2014/05/06 职场文书
土建专业毕业生自荐书
2014/07/04 职场文书
2015国庆节感想
2015/08/04 职场文书
Python之基础函数案例详解
2021/08/30 Python
Pycharm远程调试和MySQL数据库授权问题
2022/03/18 MySQL