Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace multiple spaces in a string with a single space

Tags:

regex

php

I have a file with has several spaces among the words at some point. I need to clean the file and replace existing multi-spaced sequences with one space only. I have written the following statement which does not work at all, and it seems I'm making a big mistake.

 $s = preg_replace("/( *)/", " ", $x);

My file is very simple. Here is a part of it:

Hjhajhashsh dwddd dddd sss   ddd wdd ddcdsefe xsddd   scdc yyy5ty    ewewdwdewde           wwwe ddr3r dce eggrg               vgrg fbjb   nnn  bh jfvddffv mnmb   weer ffer3ef f4r4 34t4 rt4t4t 4t4t4t4t    ffrr  rrr  ww w w ee3e iioi   hj   hmm  mmmmm mmjm lk ;’’ kjmm  ,,,, jjj hhh  lmmmlm m mmmm lklmm jlmm m
like image 962
Mostafa Talebi Avatar asked Mar 05 '14 08:03

Mostafa Talebi


People also ask

How do I remove multiple spaces from a string?

We can use replace() to remove all the whitespaces from the string.

How do I replace multiple spaces with single space in bash?

Continuing with that same thought, if your string with spaces is already stored in a variable, you can simply use echo unquoted within command substitution to have bash remove the additional whitespace for your, e.g. $ foo="too many spaces."; bar=$(echo $foo); echo "$bar" too many spaces.


1 Answers

What I usually do to clean up multiple spaces is:

while (strpos($x, '  ') !== false) {
   $x = str_replace('  ', ' ', $x);
}

Conditions/hypotheses:

  1. strings with multiple spaces are rare
  2. two spaces are by far more common than three or more
  3. preg_replace is expensive in terms of CPU
  4. copying characters to a new string should be avoided when possible

Of course, if condition #1 is not met, this approach does not make sense, but it usually is.

If #1 is met, but any of the others is not (this may depend on the data, the software (PHP version) or even the hardware), then the following may be faster:

if (strpos($x, '  ') !== false) {
   $x = preg_replace('/  +/', ' ', $x); // i.e.: '/␣␣+/'
}

Anyway, if multiple spaces appear only in, say, 2% of your strings, the important thing is the preventive check with strpos, and you probably don't care much about optimizing the remaining 2% of cases.

like image 111
Walter Tross Avatar answered Sep 21 '22 10:09

Walter Tross