我有以下字符串,我想从多个空格中“清理”:
$string = "This is a test string"; //Using utf8_decode
没什么大不了的?但是,使用后字符串不会被“清理”:
$string = preg_replace('/\s+/', ' ', $string);
因为,字符串实际上是这样的:
$test = "This is a  test string";
那么,我该如何解决这个问题?
感谢。
请,我不想替换str_replace('Â', '')
之类的字符
答案 0 :(得分:1)
您可以使用/u
UNICODE修饰符:
$string = preg_replace('/\s+/u', ' ', $string);
/u
modifier使PCRE引擎能够将字符串作为UTF8字符串处理(通过启用PCRE_UTF8
动词)并使模式中的速记字符类识别为Unicode(通过启用PCRE_UCP
动词)
重点是\s
现在将匹配所有Unicode空格,输入字符串将被视为Unicode字符串。