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 相关文章推荐
swfupload 多文件上传实现代码
Aug 27 PHP
php 文件状态缓存带来的问题
Dec 14 PHP
Uchome1.2 1.5 代码学习 common.php
Apr 24 PHP
php实现的仿阿里巴巴实现同类产品翻页
Dec 11 PHP
PHP 学习路线与时间表
Feb 21 PHP
PHP设计模式之命令模式的深入解析
Jun 13 PHP
详解PHP中instanceof关键字及instanceof关键字有什么作用
Nov 05 PHP
PHP中header函数的用法及其注意事项详解
Jun 13 PHP
php函数mkdir实现递归创建层级目录
Oct 27 PHP
Laravel 对某一列进行筛选然后求和sum()的例子
Oct 10 PHP
分享8个Laravel模型时间戳使用技巧小结
Feb 12 PHP
thinkphp框架表单数组实现图片批量上传功能示例
Apr 04 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
fleaphp crud操作之find函数的使用方法
2011/04/23 PHP
php操作MongoDB基础教程(连接、新增、修改、删除、查询)
2014/03/25 PHP
PHP把JPEG图片转换成Progressive JPEG的方法
2014/06/30 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
IE7提供XMLHttpRequest对象为兼容
2007/03/08 Javascript
js刷新框架子页面的七种方法代码
2008/11/20 Javascript
js中复制行和删除行的操作实例
2013/06/25 Javascript
浏览器窗口大小变化时使用resize事件对框架不起作用的解决方法
2014/05/11 Javascript
jQuery中prevAll()方法用法实例
2015/01/08 Javascript
使用命令对象代替switch语句的写法示例
2015/02/28 Javascript
jquery实现的用户注册表单提示操作效果代码分享
2015/08/28 Javascript
js实现字符串和数组之间相互转换操作
2016/01/12 Javascript
NodeJs的优势和适合开发的程序
2016/08/14 NodeJs
基于Vue中点击组件外关闭组件的实现方法
2018/03/06 Javascript
vue计算属性和监听器实例解析
2018/05/10 Javascript
vue组件之间通信方式实例总结【8种方式】
2019/02/22 Javascript
vue实现菜单切换功能
2019/05/08 Javascript
Python元组操作实例分析【创建、赋值、更新、删除等】
2017/07/24 Python
Django中redis的使用方法(包括安装、配置、启动)
2018/02/21 Python
Python常用字符串替换函数strip、replace及sub用法示例
2018/05/21 Python
pandas.DataFrame删除/选取含有特定数值的行或列实例
2018/11/07 Python
Python pandas如何向excel添加数据
2020/05/22 Python
Python lxml库的简单介绍及基本使用讲解
2020/12/22 Python
html5画布旋转效果示例
2014/01/27 HTML / CSS
生日派对邀请函
2014/01/13 职场文书
信息专业大学生自我评价分享
2014/01/17 职场文书
展会邀请函范文
2014/01/26 职场文书
工程采购员岗位职责
2014/03/09 职场文书
2014最新毕业证代领委托书
2014/09/26 职场文书
小学感恩节活动策划方案
2014/10/06 职场文书
中学音乐课教学反思
2016/02/18 职场文书
《女娲补天》教学反思
2016/02/20 职场文书
Mysql排序的特性详情
2021/11/01 MySQL
NodeJs使用webpack打包项目的方法详解
2022/02/28 NodeJs
详解Python中*args和**kwargs的使用
2022/04/07 Python
Windows Server 2016服务器用户管理及远程授权图文教程
2022/08/14 Servers