[英]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.