replace multiple spaces in a string with a single space
Your regex replaces any number of spaces (including zero) with a space. You should only replace two or more (after all, replacing a single space with itself is pointless):
$s = preg_replace("/ {2,}/", " ", $x);
// Your input
$str = "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";
echo $str.'<br>';
$output = preg_replace('!\s+!', ' ', $str); // Replace multispace with sigle.
echo $output;
What I usually do to clean up multiple spaces is:
while (strpos($x, ' ') !== false) {
$x = str_replace(' ', ' ', $x);
}
Conditions/hypotheses:
- strings with multiple spaces are rare
- two spaces are by far more common than three or more
preg_replace
is expensive in terms of CPU- 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.