I'm looking to replace multiple instances of space characters. My initial searches all seem to focus on using the /s
but this includes newlines and other whitespace
I think this should be close? replace two or more instances spaces " "
with one space
preg_replace('/ {2,}/', ' ', $string);
What about trying this :
preg_replace('/\s\s+/', ' ', $string);
$str = 'word word 123.'."
".'new line word';
$replaced = preg_replace('#\h{2,}#m', ' ', $str);
output:
word word 123.
new line work
The \h
escape sequences is for horizontal space only. The m
modifier is for multi line.