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. 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
And the results were for $filtered1 and $filtered2:
thisREPLACEisREPLACEaREPLACEtestREPLACE1 REPLACE2 REPLACE3 REPLACE4 REPLACE6 REPLACEend.
For all the others, the result was:
this is a test 1 2 3 4 6 end
Is like PHP is not finding the spaces, even with explode is not finding the double spaces " ". I'm using PHP 5.5.1
preg_replace('/\s+/',' ', $string);(replace all 1 or more instance of space with a single space)preg_replace('/\s{2}/',' ', $string);