If I have understood your question right, you mean spaces or all whitespace? For both, there are solutions as mentioned below:-
For just spaces, use str_replace:
$string = str_replace(' ', '', $string);
For all whitespace (including tabs and line ends), use preg_replace:
$string = preg_replace('/\s+/', '', $string);