Home | About | Categories | All Tips & Tutorials

Remove double whitespaces from a string of text using PHP preg_replace

ID: 329

Category: PHP7

Added: 18th of December 2022

Views: 1,221

If you need to remove double whitespaces from a string of text using PHP, we can use the preg_replace function.

Copy and paste the code below

<?php
echo $remove_double_spaces = preg_replace('/(\s\s+)/', ' ', 'Clean   up   this   line   of   text.');
?>


Result
Clean up this line of text.