preg_replace()是否会更改我的字符集? [英] Does preg_replace() change my character set?

查看:56
本文介绍了preg_replace()是否会更改我的字符集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下这段代码似乎正在改变我的字符集。

I have the following piece of code which seems to be changing my character set.

     $html = "à";
     echo $html;  // result: à
     $html = preg_replace("/\s/", "", $html);
     echo $html;  // result: ?

但是,当我使用 [\t\n\r \f\v] 作为我的模式,而不是特殊字符 \s ,它可以正常工作:

However, when I use [\t\n\r\f\v] as my pattern instead of the special character \s it works fine:

     $html = "à";
     echo $html;  // result: à
     $html = preg_replace("/[\t\n\r\f\v]/", "", $html);
     echo $html;  // result: à

为什么?

推荐答案

我有同样的问题。

à 0xc3a0 在UTF8中。在PHP中,您可以这样编写: \xc3\xa0

à is 0xc3a0 in UTF8. In PHP you can write like this: "\xc3\xa0".

使用PCRE / s 匹配 0xa0 就像是ASCII不间断空格。

With PCRE the /s match 0xa0 like it was ASCII "Non-breaking space".

您可以使用 u 标志解决问题。

You can use the u flag to resolve the problem.

$html = preg_replace("/\s/u", "", $html);

这篇关于preg_replace()是否会更改我的字符集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆