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;
You only need to add a literal blank space in your negated character class.
It would be more concise to use:
i
and omit one of the letter ranges[0-9]
as \d
like this:
if (preg_match('/[^A-Z0-9_ -]/i', $str)) return FALSE;
but even better would be to use \w
which represents [A-Za-z0-9_] (which is most of your pattern) to write:
if (preg_match('/[^\w -]/', $str)) return FALSE;
This pattern states that if $str
contains any character not in [A-Za-z0-9_ -]
then false
will be returned.