Category: PHP7

Added: 18th of December 2022

Viewed: 1,081 times


Remove double whitespaces from a string of text using PHP preg_replace

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.