【问题标题】:Why does' PHP's str_replace() cause encoding issues?为什么 PHP 的 str_replace() 会导致编码问题?
【发布时间】:2012-04-03 14:51:44
【问题描述】:

我有一个字符串,我正在通过 str_replace() 传递,它似乎对我无法弄清楚的编码有一些影响。

示例:

$str = "joined nearly 500 of the world’s investors .."; //shorted exceprt
$str = str_replace(' ', ' ', $str);
var_dump($str);

给予:

joined nearly 500 of the worldÂ’s investors

知道如何防止这种情况发生吗?

【问题讨论】:

  • 你有一个聪明的报价,而不是一个标准的撇号。
  • 你确定是你的 PHP 出了问题吗?您的 HTML 文件是否正确配置了编码?
  • 在这种情况下使用utf8_encode

标签: php character-encoding iso-8859-1


【解决方案1】:

在您的输入中,您有一个不在其实体中的智能引用!另外,您可能想使用 UTF-8,所以试试这个:

$str = "joined nearly 500 of the world’s investors .."; //shorted exceprt
$str = htmlentities($str, ENT_QUOTES, "UTF-8");
$str = str_replace(' ', ' ', $str);
var_dump($str);

【讨论】:

    【解决方案2】:

    这与 str_replace 或 PHP 无关,而是 HTML/浏览器字符集编码。

    您可以这样做(在 PHP 中,在其他任何事情之前):

    header('Content-Type: text/html; charset=utf-8');
    

    或者(在您的 HTML 的 head 部分 - 这里是 HTML 5):

    <meta charset="utf-8">
    

    另一种选择是更改浏览器的默认编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      相关资源
      最近更新 更多