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

为什么';PHP';s str_replace()导致编码问题?,php,character-encoding,iso-8859-1,Php,Character Encoding,Iso 8859 1,我有一个字符串,我正在通过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 你知道如何防止这种情况吗?在你的

我有一个字符串,我正在通过
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\u replace
或PHP无关,而是与HTML/浏览器字符集编码有关

您可以这样做(在PHP中,在执行任何其他操作之前):

或者(在HTML-HTML5的
标题部分):



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

您使用的是智能引号而不是标准撇号。您确定是您的PHP出错了吗?HTML文件的编码配置是否正确?在这种情况下,请使用
utf8\u encode
header('Content-Type: text/html; charset=utf-8');
<meta charset="utf-8">