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 相关文章推荐
自动跳转中英文页面
Oct 09 PHP
PHP 截取字符串专题集合
Aug 19 PHP
Zend Guard使用指南及问题处理
Jan 07 PHP
php正则匹配html中带class的div并选取其中内容的方法
Jan 13 PHP
php二维数组合并及去重复的方法
Mar 04 PHP
php实现用于计算执行时间的类实例
Apr 18 PHP
浅谈PHP中foreach/in_array的使用
Nov 02 PHP
Linux系统中为php添加pcntl扩展
Aug 28 PHP
CentOS 7.2 下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法详解(mini版本)
Sep 01 PHP
PHP cURL获取微信公众号access_token的实例
Apr 28 PHP
ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解
Apr 09 PHP
Laravel框架实现多数据库连接操作详解
Jul 12 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常用函数 推荐收藏保存
2010/02/21 PHP
php文件上传简单实现方法
2015/01/24 PHP
PHP flush 函数使用注意事项
2016/08/26 PHP
Yii2实现增删改查后留在当前页的方法详解
2017/01/13 PHP
PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
2017/12/21 PHP
jquery下为Event handler传递动态参数的代码
2011/01/06 Javascript
JS实现的Select三级下拉菜单代码
2015/08/20 Javascript
基于Vue实现支持按周切换的日历
2020/09/24 Javascript
详解用函数式编程对JavaScript进行断舍离
2017/09/18 Javascript
AngularJS实时获取并显示密码的方法
2018/02/06 Javascript
Element Table的row-class-name无效与动态高亮显示选中行背景色
2018/11/30 Javascript
vuedraggable+element ui实现页面控件拖拽排序效果
2020/07/29 Javascript
详解vue2.6插槽更新v-slot用法总结
2019/03/09 Javascript
jQuery实现简单飞机大战
2020/07/05 jQuery
Python判断值是否在list或set中的性能对比分析
2016/04/16 Python
python中string模块各属性以及函数的用法介绍
2016/05/30 Python
在Python web中实现验证码图片代码分享
2017/11/09 Python
django_orm查询性能优化方法
2018/08/20 Python
Python访问MongoDB,并且转换成Dataframe的方法
2018/10/15 Python
对numpy中二进制格式的数据存储与读取方法详解
2018/11/01 Python
python给指定csv表格中的联系人群发邮件(带附件的邮件)
2019/12/31 Python
踩坑:pytorch中eval模式下结果远差于train模式介绍
2020/06/23 Python
python如何使用腾讯云发送短信
2020/09/17 Python
video实现有声音自动播放的实现方法
2020/05/20 HTML / CSS
Interrail法国:乘火车探索欧洲,最受欢迎的欧洲铁路通票
2019/08/27 全球购物
物业门卫岗位职责
2013/12/28 职场文书
入党积极分子思想汇报
2014/01/02 职场文书
晚归检讨书
2014/02/19 职场文书
2014单位领导班子四风对照检查材料思想汇报
2014/09/25 职场文书
教师节标语大全
2014/10/07 职场文书
小学生节水倡议书
2015/04/29 职场文书
2015年教导处教学工作总结
2015/07/22 职场文书
党员读书活动心得体会
2016/01/14 职场文书
HashMap实现保存两个key相同的数据
2021/06/30 Java/Android
mysql中int(3)和int(10)的数值范围是否相同
2021/10/16 MySQL
JavaScript实现两个数组的交集
2022/03/25 Javascript