[英]Remove multiple whitespaces - PHP doesn't find the spaces
I know, there is a lot of similar questions, and you will says, is a duplicate, but I can't find the solution! 我知道,有很多类似的问题,而且您会说这是重复的,但我找不到解决方案! I need to remove multiple white spaces and only write one space.
我需要删除多个空格,而只写一个空格。 In my code I wrote 'REPLACE' instead of ' ', just to clarify.
为了清楚起见,在我的代码中,我写了“ REPLACE”而不是“”。 This is some code that I have tested and is not working:
这是我测试过的一些代码,无法正常工作:
$string=$data['post_content'];
$filtered1=preg_replace("/[^\S\r\n]+/",'REPLACE',$string);
$filtered2=preg_replace("#[^\S\r\n]+#",'REPLACE',$string);
$filtered3=preg_replace("#[^\S\r\n][^\S\r\n]+#",'REPLACE',$string);
$filtered4=preg_replace('#[^\S\r\n][^\S\r\n]+#','REPLACE',$string);
$filtered5=preg_replace('#!\s+!#', 'REPLACE', $string);
$filtered6=preg_replace("/(?<=\s)\x20|\x20(?=\s)/", "REPLACE", $string);
$filtered7=preg_replace("/([\s])\1+/", "REPLACE", $string);
$filtered8=preg_replace("#[^\S\r\n][^\S\r\n]#",'REPLACE',$string);
$filtered9=preg_replace("'/\s+/'",'REPLACE',$string);
$testing1=str_replace(" ","REPLACE",$string);
$testing2=str_replace("\s",'REPLACE',$string);
$testing3=str_replace(array(' '),'REPLACE',$string);
$testing4=str_replace(' ',"REPLACE",$string);
$testing5=str_replace(" ","REPLACE",$string);
$testing6=str_replace(array(" "),'REPLACE',$string);
$testing7=str_replace(array("\s\s"),'REPLACE',$string);
This is the string test: 这是字符串测试:
this is a test 1 2 3 4 6 end
这是一个测试1 2 3 4 6结束
And the results were for $filtered1
and $filtered2
: 结果是
$filtered1
和$filtered2
:
thisREPLACEisREPLACEaREPLACEtestREPLACE1 REPLACE2 REPLACE3 REPLACE4 REPLACE6 REPLACEend.
thisREPLACEisREPLACEaREPLACEtestREPLACE1 REPLACE2 REPLACE3 REPLACE4 REPLACE6 REPLACEend。
For all the others, the result was: 对于其他所有人,结果是:
this is a test 1 2 3 4 6 end
这是一个测试1 2 3 4 6结束
Is like PHP is not finding the spaces, even with explode
is not finding the double spaces " ". 就像PHP找不到空格,甚至用
explode
也不找到双空格“”。 I'm using PHP 5.5.1 我正在使用PHP 5.5.1
Here it goes use 在这里有用
preg_replace('/[^\\S\\r\\n]{2,}/',' ',$string);
to convert dual spaces to single one 将双空格转换为单空格
see demo here : http://regex101.com/r/sP7wH7/1 在此处查看演示: http : //regex101.com/r/sP7wH7/1
But I'll rather be using the simplest preg_replace('/ {2,}/',' ',$string);
但是我宁愿使用最简单的
preg_replace('/ {2,}/',' ',$string);
to complete this 完成这个
Your test string has non-breaking spaces, which are not picked up by \\s
in your regex pattern. 您的测试字符串具有不间断空格,正则表达式模式中的
\\s
不会拾取空格。 Use this instead: 使用此代替:
preg_replace('/(\\s+)|(\\xA0+)/u', ' ', $string);
The problem wasn't the RegEx, the problem was the modifier, I used the modifier u
thanks to Aurimas answer. 问题不是RegEx,而是修饰符,感谢Aurimas的回答,我使用了修饰符
u
。 These are the possible solutions: 这些是可能的解决方案:
$filtered1=preg_replace("/[^\S\r\n]+/u",' ',$string);
$filtered2=preg_replace("#[^\S\r\n]+#u",' ',$string);
$filtered3=preg_replace("#[^\S\r\n][^\S\r\n]+#u",' ',$string);
$filtered10=preg_replace('/[^\S\r\n]{2,}/u',' ',$string);0
$filtered11=preg_replace('/\h+/u', ' ', $string);
$filtered16=preg_replace('/(\xA0+)/u', ' ', $string);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.