Logo

Programming-Idioms

History of Idiom 219 > diff from v21 to v22

Edit summary for version 22 by karololszacki:
New PHP implementation by user [karololszacki]

Version 21

2020-11-22, 22:28:45

Version 22

2021-03-19, 14:06:20

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Extra Keywords
collapse repeated
Extra Keywords
collapse repeated
Code
$t = preg_replace('/\s+/', ' ', $s);
Comments bubble
replaces all whitespace
Doc URL
https://www.php.net/manual/en/function.preg-replace
Demo URL
https://3v4l.org/Z6LWI