如何在 PHP 中替换 Microsoft 编码的引号 [英] How to replace Microsoft-encoded quotes in PHP

查看:18
本文介绍了如何在 PHP 中替换 Microsoft 编码的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的应用程序中的编码问题,我需要将 Microsoft Word 版本的单引号和双引号 ("" ' ') 替换为正则引号(' 和 ").我不需要它们是 HTML 实体,我无法更改我的数据库架构.

我有两个选择:使用正则表达式或关联数组.

有没有更好的方法来做到这一点?

解决方案

考虑到您只想替换一些特定且明确标识的字符,我会选择 str_replace 带数组:你显然不需要重炮正则表达式会给你带来;-)

如果您遇到一些其他特殊字符(该死的从 Microsoft Word 复制粘贴...),您可以在需要时/在识别时将它们添加到该数组中.


我能给你的评论最好的答案可能是这个链接:ConvertPHP 智能引号

以及相关代码(引用该页面):

function convert_smart_quotes($string){$search = array(chr(145),铬(146),铬(147),铬(148),字符(151));$replace = array("'","'",'"','"','-');返回 str_replace($search, $replace, $string);}

(我这台电脑上没有Microsoft Word,所以无法自己测试)

我不记得我们在工作中使用的确切内容(我不是必须处理那种输入的人),但它们是同一种东西......>

I need to replace Microsoft Word's version of single and double quotations marks (" " ‘ ’) with regular quotes (' and ") due to an encoding issue in my application. I do not need them to be HTML entities and I cannot change my database schema.

I have two options: to use either a regular expression or an associated array.

Is there a better way to do this?

解决方案

Considering you only want to replace a few specific and well identified characters, I would go for str_replace with an array: you obviously don't need the heavy artillery regex will bring you ;-)

And if you encounter some other special characters (damn copy-paste from Microsoft Word...), you can just add them to that array whenever is necessary / whenever they are identified.


The best answer I can give to your comment is probably this link: Convert Smart Quotes with PHP

And the associated code (quoting that page):

function convert_smart_quotes($string) 
{ 
    $search = array(chr(145), 
                    chr(146), 
                    chr(147), 
                    chr(148), 
                    chr(151)); 

    $replace = array("'", 
                     "'", 
                     '"', 
                     '"', 
                     '-'); 

    return str_replace($search, $replace, $string); 
} 

(I don't have Microsoft Word on this computer, so I can't test by myself)

I don't remember exactly what we used at work (I was not the one having to deal with that kind of input), but it was the same kind of stuff...

这篇关于如何在 PHP 中替换 Microsoft 编码的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆