I\'m such an amateur at regex, how do I allow spaces(doesn\'t matter how many) in this regex?
if(preg_match(\'/[^A-Za-z0-9_-]/\', $str)) return FALSE;
if(preg_match('/[^A-Za-z0-9_ -]/', $str)) return FALSE;
Note that I put the space before the hyphen. If the space were after the hyphen, I would be specifying a character range from underscore to space. (Issue also evadable by putting a backslash before the hyphen to escape it.)
This is assuming that what you mean by "allow" is: this regex is being used to validate a character string, and if it matches, then the character string is disallowed (hence return FALSE). So the characters in the negated character class ([^...]) are actually the allowed characters. (This is causing some general confusion in this question.)