繁体   English   中英

为什么'PHP的str_replace()导致编码问题?

[英]Why does' PHP's str_replace() cause encoding issues?

我有一个字符串,我通过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

任何想法如何防止这种情况?

在您的输入中,您有一个不在其实体中的智能报价! 另外,您可能想要使用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);

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

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

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

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

<meta charset="utf-8">

另一种方法是更改​​浏览器的默认编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM