Originally posted by seanlyon
preg_match("/[a-zA-Z0-9-_\s]+/i", $body, $result);
I didn't test that, but I highly doubt it works as desired. There are a few reasons for my saying this:
1) There is no ^ at the beginning and $ at the end to tell it to match from the beginning of the string to the end.
2) a - in the middle of a character class (between the [ and the ]) mean it is metacharacter (specifically a class range). You will want to either put a backslash in front of it, or put it at the beginning or end of your character class (as then it will be interpreted literally).
3) \s matches all spaces, including \t (tab), \n (new line), and \r (carriage return). This may or may not be a big deal, but since they asked about spaces specifically, I thought I'd mention it.
Try this one (which matches exactly as you asked, assuming that spaces means spaces and not things such as tabs, etc):
/^[-\w ]+$/i
If you want to be positive it contains at least one alphanumeric character (the above will allow that character to be a -, _ or space, the easiest way would be to just run it through another regexp...