How to replace all but first (white)spaces by
when more than one space?
Specifically requested for use with php's preg_replace
, so PCRE.
"This is my text."
Should be converted into
"This is my text."
It seems all you need is to replace each whitespace that is preceded with another whitespace symbol. Use a lookbehind-based approach:
(?<=\s)\s
See the regex demo.
The (?<=\s)
is a positive lookbehind that requires the presence of a whitespace immediately before the current location, but the whitespace is not consumed, and is thus not replaced.
Below is a PHP demo:
$s = "This is my text.";
echo preg_replace('~(?<=\s)\s~', ' ', $s);
// => This is my text.
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