PHP字符串word末字符实现大小写互换的方法

 更新时间:2014年11月10日 09:56:07   投稿:shichen2014  
这篇文章主要介绍了PHP字符串word末字符实现大小写互换的方法,是涉及PHP字符串转换非常实用的技巧,需要的朋友可以参考下

本文实例讲述了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 多继承的几种常见实现方法示例

    这篇文章主要介绍了php 多继承的几种常见实现方法,结合实例形式分析了两种PHP实现多继承的操作方法,需要的朋友可以参考下
    2019-11-11
  • PHP实现基于文本的简易搜索引擎功能

    PHP实现基于文本的简易搜索引擎功能

    这篇文章给大家介绍了PHP实现基于文本的简易搜索引擎功能,让这个功能可以在小型网站或者特定数据集内提供快速的关键字搜索能力,非常适合没有使用复杂数据库搜索引擎(如Elasticsearch)的场景,需要的朋友可以参考下
    2024-02-02
  • 关于IIS php调用com组件的权限问题

    关于IIS php调用com组件的权限问题

    Word的对象库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB)(这是针对老版本的情况,在用vs.net2005的时候,直接在引用对话框中,在com组件里找到对word的库文件的引用就可以了,文件名好像是一样的
    2012-01-01
  • ThinkPHP开发框架函数详解:C方法

    ThinkPHP开发框架函数详解:C方法

    这篇文章主要给大家讲解ThinkPHP开发框架函数详解:C方法,有需要的朋友可以参考下
    2015-08-08
  • PHP 中关于ord($str)&gt;0x80的详细说明

    PHP 中关于ord($str)&gt;0x80的详细说明

    为了识别双字节的字符,比如汉字或日文韩文等都是占两字节的,每字节高位为1,而一般西文字符只有一个字节,七位有效编码,高位为0而0x80对应的二进制代码为1000 0000,最高位为一,代表汉字.汉字编码格式通称为10格式. 一个汉字占2字节,但只代表一个字符
    2012-09-09
  • PHP设计模式之简单投诉页面实例

    PHP设计模式之简单投诉页面实例

    这篇文章主要为大家详细介绍了PHP设计模式下简单投诉页面实例,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • 在php MYSQL中插入当前时间

    在php MYSQL中插入当前时间

    mysql时间函数
    2008-04-04
  • php实现删除指定目录下相关文件的方法

    php实现删除指定目录下相关文件的方法

    这篇文章主要介绍了php实现删除指定目录下相关文件的方法,主要涉及对文件的遍历以及对文件的各种常用操作,需要的朋友可以参考下
    2014-10-10
  • php实现URL加密解密的方法

    php实现URL加密解密的方法

    这篇文章主要介绍了php实现URL加密解密的方法,结合实例形式分析了php针对URL字符串进行加密解密操作的相关技巧,需要的朋友可以参考下
    2016-11-11
  • php实现生成code128条形码的方法详解

    php实现生成code128条形码的方法详解

    这篇文章主要介绍了php实现生成code128条形码的方法,结合完整实例形式给出了php条形码生成类的定义与使用方法,需要的朋友可以参考下
    2017-07-07

最新评论