I think you are close, it is just that preg_replace returns the modified string, so you'd need to assign it to something:
$string = "this is a test";
$string = preg_replace("/\\s/", "", $string);
die($string);
(I didn't test it, but I think this should work)