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
                We can use replace() to remove all the whitespaces from the string.
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.
What I usually do to clean up multiple spaces is:
while (strpos($x, '  ') !== false) {
   $x = str_replace('  ', ' ', $x);
}
Conditions/hypotheses:
preg_replace is expensive in terms of CPUOf 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With