Logo

Programming-Idioms

History of Idiom 219 > diff from v22 to v23

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

Version 22

2021-03-19, 14:06:20

Version 23

2021-03-19, 14:10:07

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 = $s;
do $t = str_replace('  ', ' ', $t, $count); while($count);
Comments bubble
only spaces are handled

might not be very efficient, but does not use regexes
Doc URL
https://www.php.net/manual/en/function.str-replace.php
Origin
https://www.php.net/manual/en/function.str-replace.php#125165
Demo URL
https://3v4l.org/6AWIt