How can whitespace be efficiently removed from a string in PHP without affecting single spaces?

When removing whitespace from a string in PHP, we can use the `trim()` function to remove leading and trailing whitespace. However, if we want to preserve single spaces within the string, we can use the `preg_replace()` function with a regular expression pattern to replace multiple whitespace characters with a single space.

$string = "   Hello    World   ";
$cleaned_string = preg_replace('/\s+/', ' ', trim($string));
echo $cleaned_string;