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

本文实例讲述了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程序设计有所帮助。

(0)

相关推荐

  • 使用PHP导出Word文档的原理和实例

    原理 一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方法.安装过office的服务器可以调用一个叫word.application的com,可以生成word文档,不过这种方式我不推荐,因为执行效率比较低(我测试了一下,在执行代码的时候,服务器会真的去打开一个word客户端).理想的com应该是没有界面的,在后台进行数据转换,这样效果会比较好,但是这些扩展一般需要收费.第2种方法,就是用PHP将我们的doc文档内容直接写

  • php通过baihui网API实现读取word文档并展示

    项目中遇到一个小问题,想实现php 如何读取word文档,并将其内容原样显示 可以 使用API 可以看看baihui.com 的写写应用 的API 申请一个 APPKEY 就能使用,你可以看看 ... 对免费版本有限制 比如 excel 支持,可以参考我这个 appkey是我申请的,可以使用吧 ... 保存成本地的一个html文件 打开后直接使用 word 的类似 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&q

  • PHP中将网页导出为Word文档的代码

    一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方法.安装过office的服务器可以调用一个叫word.application的com,可以生成word文档,不过这种方式我不推荐,因为执行效率比较低(我测试了一下,在执行代码的时候,服务器会真的去打开一个word客户端).理想的com应该是没有界面的,在后台进行数据转换,这样效果会比较好,但是这些扩展一般需要收费. 第2种方法,就是用PHP将我们的doc文档内容直接写入一

  • php导出word文档与excel电子表格的简单示例代码

    生成word的代码: 复制代码 代码如下: header("Content-type: application/octet-stream"); header("Accept-Ranges: bytes"); header('Content-type: application/doc'); header('Content-Disposition: attachment; filename="测试.doc"'); 生成excel的代码 : 复制代码

  • php实现word转html的方法

    本文实例讲述了php实现word转html的方法.分享给大家供大家参考,具体如下: 要想完美解决,office转pdf或者html,最好还是用windows office软件,libreoffice不能完美转换,wps没有api. 先确认com模块是不是开启,phpinfo里面如果有com_dotnet模块,说明已开启,如果没有,修改php.ini, 复制代码 代码如下: com.allow_dcom = true 前面的注释去掉,重启就OK了,php官方网站说,php5.4.5之前,com模块

  • php导出word格式数据的代码实例

    本节内容:一个php导出文档的类 例子: 复制代码 代码如下: <?php /*** 生成word文档的类* */class word{     function start()    {        ob_start();        echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"        xmlns:w="urn:schemas-microsoft-com:office:wo

  • php 备份数据库代码(生成word,excel,json,xml,sql)

    单表备份代码: 复制代码 代码如下: <?php    class Db    {        var $conn; function Db($host="localhost",$user="root",$pass="root",$db="test")        {          if(!$this->conn=mysql_connect($host,$user,$pass))          die(

  • php在程序中将网页生成word文档并提供下载的代码

    在这篇文章中主要解决两个问题: 1:在php中如何把html中的内容生成到word文档中 2:php把html中的内容生成到word文档中时,不居中显示问题,即会默认按照web视图进行显示. 3:php把html中的内容生成到word文档中时,相关样式不兼容问题 正文: 复制代码 代码如下: echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microso

  • php实现将上传word文件转为html的方法

    本文实例讲述了php实现将上传word文件转为html的方法.分享给大家供大家参考.具体实现方法如下: 上传页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml

  • PHP去掉从word直接粘贴过来的没有用格式的函数

    一般处理的方式有二种:1.通过编辑器的JS直接去除.2.提交到后台后,直接用程序去掉无效标签.下面我就分享一个通过PHP的处理方式,成功率可能不是100%.这程序也是在PHP官网上看到的,就顺便粘贴过来了. 复制代码 代码如下: function ClearHtml($content,$allowtags='') { mb_regex_encoding('UTF-8'); //replace MS special characters first $search = array('/‘/u',

  • PHP把网页保存为word文件的三种方法

    一.PHP生成word的两种思路或原理 1.利用windows下面的 com组件2.利用PHP将内容写入doc文件之中具体实现方法如下. 二.利用windows下面的com组件 原理:com作为PHP的一个扩展类,安装过office的服务器会自动调用word.application的com,可以自动生成文档,PHP官方文档手册:http://www.php.net/manual/en/class.com.php 使用官方实例: 复制代码 代码如下: <?php// starting word$w

  • php导出生成word的方法

    本文实例讲述了php导出生成word的方法.分享给大家供大家参考,具体如下: PHP导出word (1)首先,预览html页面,示例化对象,定义要导出的数据 (2)点击下载页面,给id传值(任何值均可,仅用于判断),如果id有值,输出缓冲文件,保存为word格式. (3)点击下载后,(如果是图片的话,在保存为word时要使用绝对路径,这样才可以在保存的word中正常显示) (4)关闭缓存输出 Word_con.php  预览要导出的html文件 <?php if(@$_GET[id]!='')

随机推荐