See the following code:
$str = 'This string has many spaces';
$str = preg_replace('/\s+/', ' ', $str);
print $str;
output
This string has many spaces
Simply Explaining Technology
See the following code:
$str = 'This string has many spaces';
$str = preg_replace('/\s+/', ' ', $str);
print $str;
output
This string has many spaces