gpt4 book ai didi

php - preg_replace() 会改变我的字符集吗?

转载 作者:可可西里 更新时间:2023-11-01 12:49:50 25 4
gpt4 key购买 nike

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

     $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: à

这是为什么?

最佳答案

我也遇到了同样的问题。这是因为UTF8。

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

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

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

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

关于php - preg_replace() 会改变我的字符集吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19629893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com