[英]How to replace all spaces which are not actually spaces with regex in PHP
我有以下要从多个空格“清除”的字符串:
$string = "This is a test string"; //Using utf8_decode
没什么大不了的吗? 但是,在使用以下字符串后,该字符串不会被“清除”:
$string = preg_replace('/\s+/', ' ', $string);
因为,字符串实际上是这样的:
$test = "This is a  test string";
那么,如何解决此问题?
谢谢。
拜托,我不想替换诸如str_replace('Â', '')
类的Â字符
您可以使用/u
UNICODE修饰符:
$string = preg_replace('/\s+/u', ' ', $string);
/u
修饰符使PCRE引擎能够将字符串作为UTF8字符串处理(通过打开PCRE_UTF8
动词),并使模式中的速记字符类能够PCRE_UCP
Unicode(通过启用PCRE_UCP
动词)
要点是\\s
现在将匹配所有Unicode空格,并且输入字符串被视为Unicode字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.