The problem with planetsim's post is that it's too limited in scope. It accounts for spaces, but what about tabs? New Lines? Underlines? Dashes? Quotes?
There are a lot of characters that can cause problems, not just spaces.
The answer is to define what you will ACCEPT and then reject anything else. This is best done with an expression of some kind. Here's some snippets using preg:
match not (a-z, 0-9, ., _, -)
$match="/[.-_A-Za-z0-9]/";
if (preg_replace($match, '', $in)!=$in)
die ('Invalid characters have been input');
or, if you'd rather "fix it" and continue,
$in=preg_replace($match, '', $in);
.... continue on...
For security purposes, this is one of the best ways to go.