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;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.)