January 30, 2021
Here’s how you can use Laravel’s Str
helper to remove double spaces from a string in PHP, using regex:
use Illuminate\Support\Str;
$string = 'Here is my sentence. There are double spaces.';
$newString = Str::of($string)->replaceMatches('/ {2,}/', ' ');
This takes any part of the string that matches {2,}
(a space character occurring two or more times in a row) and replaces it with a single space.