It’s much easier to remove spaces in php with a simple for loop.

    function whitespace($string) {
        $array = explode(" ", $string);
        $output = "";

        for ($i=0; $i<sizeof($array); $i++) {
          if ($array[$i] != "") {
            $output = $output." ".$array[$i];
          }
        }
        return trim($output);
    }

i must admit this looks gross, but trust me it works like a charm. All those preg_replace and pattern matching in php never worked for me as well as this does.