str_replace来自word的双引号

时间:2011-12-08 19:45:46

标签: php str-replace double-quotes

我将json数据作为传递给javascript的字符串。在字符串传递之前,我正在php中搜索所有双引号并替换它们。这工作正常,但一些json字符串(看起来像)MS Word样式双引号,可能斜体。所以我的<?php $t = str_replace("”", "", $t); ?>方法调用没有转义特殊的双引号。

我是否需要找到charcode并逃脱?我尝试将引号拼接出字符串,然后将其粘贴到php方法中,但它仍然不会将字符识别为不同的双引号。

让我看看我是否可以在此处粘贴引文 - < “ >< ” >

谢谢。

2 个答案:

答案 0 :(得分:4)

<?php
function mb_str_replace($needle, $replacement, $haystack) {
   return implode($replacement, mb_split($needle, $haystack));
}
$t = "as“da”sd";
$t = mb_str_replace("”", "", $t); 
$t = mb_str_replace("“", "", $t); 
#and all the other weird quotes :)
echo $t;
?>

http://php.net/manual/en/ref.mbstring.php

http://www.regular-expressions.info/unicode.html

我建议使用preg_replace代替

$t = "as“da”sd";
$t = preg_replace("/[”“]/u","",$t); #just create a character class
echo $t;

这也可能有用: http://axonflux.com/handy-regexes-for-smart-quotes

答案 1 :(得分:1)

我自己尝试过,所以我只能提出你必须使用UTF-8编码。

<?php
    header('content-type: text/html; charset=utf-8');
    $str = "“ > and < ”\"";
    $replaceArr =  array("“", "”", "\"");
    $replaced = str_replace($replaceArr,"",$str);
    echo $replaced;
?>

当我尝试时,看起来很干净。

相关问题