preg_replace()是否会更改我的字符集?

时间:2013-10-28 08:16:15

标签: php regex encoding character-encoding special-characters

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

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

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

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

为什么?

1 个答案:

答案 0 :(得分:11)

我有同样的问题。这是因为UTF8。

UTF8中的

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

对于PCRE,/s匹配0xa0就像是ASCII“非破坏空间”。

您可以使用u flag解决问题。

$html = preg_replace("/\s/u", "", $html);
相关问题