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 相关文章推荐
BBS(php &amp; mysql)完整版(七)
Oct 09 PHP
PHP生成静态页面详解
Nov 19 PHP
php 备份数据库代码(生成word,excel,json,xml,sql)
Jun 23 PHP
PHP利用APC模块实现文件上传进度条的方法
Jan 26 PHP
PHP输出两个数字中间有多少个回文数的方法
Mar 23 PHP
php socket通信(tcp/udp)实例分析
Feb 14 PHP
php 类中的常量、静态属性、非静态属性的区别
Apr 09 PHP
php+mysql+ajax实现单表多字段多关键词查询的方法
Apr 15 PHP
PHP 的Opcache加速的使用方法
Dec 29 PHP
PHP使用两个栈实现队列功能的方法
Jan 15 PHP
php文件上传原理与实现方法详解
Dec 20 PHP
分享几种好用的PHP自定义加密函数(可逆/不可逆)
Sep 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
应用开发中涉及到的css和php笔记分享
2011/08/02 PHP
php class中public,private,protected的区别以及实例分析
2013/06/18 PHP
PHP exif扩展方法开启详解
2014/07/28 PHP
使用 PHPStorm 开发 Laravel
2015/03/24 PHP
JavaScript的面向对象(一)
2006/11/09 Javascript
JavaScript CSS菜单功能 改进版
2008/12/20 Javascript
基于jQuery的消息提示插件 DivAlert之旅(二)
2010/04/01 Javascript
关于递归运算的顺序测试代码
2011/11/30 Javascript
jQuery如何实现点击页面获得当前点击元素的id或其他信息
2014/01/09 Javascript
jquery.post用法之type设置问题
2014/02/24 Javascript
javascript使用正则表达式检测IP地址
2014/12/03 Javascript
jQuery插件Skippr实现焦点图幻灯片特效
2015/04/12 Javascript
JS基于Mootools实现的个性菜单效果代码
2015/10/21 Javascript
Angularjs 制作购物车功能实例代码
2016/09/14 Javascript
使用jquery如何获取时间
2016/10/13 Javascript
浅析JavaScript中作用域和作用域链
2016/12/06 Javascript
AngularJS实现路由实例
2017/02/12 Javascript
Angular 2.x学习教程之结构指令详解
2017/05/25 Javascript
vue实现仿淘宝结账页面实例代码
2017/11/08 Javascript
ActiveX控件的使用-js实现打印超市小票功能代码详解
2017/11/22 Javascript
JavaScript简单实现关键字文本搜索高亮显示功能示例
2018/07/25 Javascript
浅谈vue 二级路由嵌套和二级路由高亮问题
2020/08/06 Javascript
[02:32]DOTA2英雄基础教程 美杜莎
2014/01/07 DOTA
[52:20]VP vs VG Supermajor小组赛 B组胜者组决赛 BO3 第一场 6.2
2018/06/03 DOTA
Python中用于转换字母为小写的lower()方法使用简介
2015/05/19 Python
Python实现PS图像调整黑白效果示例
2018/01/25 Python
win7 x64系统中安装Scrapy的方法
2018/11/18 Python
Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】
2019/10/11 Python
HTML5 Canvas阴影使用方法实例演示
2013/08/02 HTML / CSS
精灵市场:Pixie Market
2019/06/18 全球购物
介绍一下JMS编程步骤
2015/09/22 面试题
学校门卫管理制度
2014/01/30 职场文书
房屋授权委托书范本
2014/10/07 职场文书
大学优秀学生主要事迹材料
2015/11/04 职场文书
pytorch 6 batch_train 批训练操作
2021/05/28 Python
Windows Server 版本 20H2 于 8 月 9 日停止支持,Win10 版本 21H1 将于 12 月结束支
2022/07/23 数码科技