Replacing Multiple Spaces with a Single Space in PHP


Condense multiple spaces in a string into a single space.

Source Code

$text = "This    is    a    text    with    multiple    spaces.";
$cleaned = preg_replace('/s+/', ' ', $text);
echo $cleaned; // Outputs: "This is a text with multiple spaces."
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments