0

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

4
  • if you want to remove all multiple spaces you you can use preg_replace('/\s+/',' ', $string); (replace all 1 or more instance of space with a single space) Commented Sep 21, 2014 at 16:45
  • Is still returning the same result: "this is a test 1 2 3 4 6 end" Commented Sep 21, 2014 at 17:43
  • what is your expected output? and you want to match exactly 2 spaces you can use preg_replace('/\s{2}/',' ', $string); Commented Sep 22, 2014 at 4:00
  • For some weird reason, in the comments the result is looking fine. That is how it should look Commented Sep 22, 2014 at 11:15

3 Answers 3

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

But I'll rather be using the simplest preg_replace('/ {2,}/',' ',$string); to complete this

Sign up to request clarification or add additional context in comments.

1 Comment

I know the regex should work, but is still returning the same result: this is a test 1 2 3 4 6 end
1

Your test string has non-breaking spaces, which are not picked up by \s in your regex pattern. Use this instead:

preg_replace('/(\s+)|(\xA0+)/u', ' ', $string);

1 Comment

I needed to keep the new lines characters, but your answer was useful to know the modifier "u"
0

The problem wasn't the RegEx, the problem was the modifier, I used the modifier u thanks to Aurimas answer. 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.