[英]Why does' PHP's str_replace() cause encoding issues?
I have a string that I am passing through str_replace()
, and it seems to have some effect on encoding that I cannot figure out. 我有一个字符串,我通过str_replace()
,它似乎对编码有一些影响,我无法弄清楚。
example: 例:
$str = "joined nearly 500 of the world’s investors .."; //shorted exceprt
$str = str_replace(' ', ' ', $str);
var_dump($str);
gives: 得到:
joined nearly 500 of the worldÂ’s investors
any idea how to prevent this? 任何想法如何防止这种情况?
In your input, you have a smart quote that is not in its entity! 在您的输入中,您有一个不在其实体中的智能报价! Plus, you probably want to use UTF-8 so try this: 另外,您可能想要使用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);
That has nothing to do with str_replace
or PHP, but rather HTML/browser charset encoding. 这与str_replace
或PHP无关,而是与HTML /浏览器字符集编码无关。
You can either do (in PHP, before anything else): 你可以这样做(在PHP中,在其他任何事情之前):
header('Content-Type: text/html; charset=utf-8');
Or (in the head
section of your HTML - HTML 5 here): 或者(在HTML的head
部分 - 这里是HTML 5):
<meta charset="utf-8">
Another alternative would be to change your browsers default encoding. 另一种方法是更改浏览器的默认编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.