RopeADope Posted June 29, 2010 Share Posted June 29, 2010 My match statement if(preg_match('"[^A-Za-z0-9]"', $user)){ echo "Your login contains invalid characters."; } Q1: I couldn't get this to work until I put single AND double quotes around the expression. Why is that? Q2: Please clarify my understanding of this statement if I'm wrong. "if $user contains characters other than what's in the expression, echo 'Your login contains invalid characters.'" Q3: The end goal is to check a username from a $_POST variable and if it contains any non-alphanumeric characters, kickback to the login. Will preg_match catch characters like \n \r : ; etc.? Quote Link to comment https://forums.phpfreaks.com/topic/206183-questions-on-preg_match/ Share on other sites More sharing options...
salathe Posted June 29, 2010 Share Posted June 29, 2010 Q1: PCRE requires that you use pattern delimiters: http://php.net/regexp.reference.delimiters Q2: More like "if $user matches the expression, echo..." (if $user contains any one non-alphanumeric character, echo...) Q3: Yes. Quote Link to comment https://forums.phpfreaks.com/topic/206183-questions-on-preg_match/#findComment-1078731 Share on other sites More sharing options...
cags Posted June 29, 2010 Share Posted June 29, 2010 A3: What he said. But bare in mind that an empty string would be considered valid by that Regex. You could also use something like ctype_alnum. Quote Link to comment https://forums.phpfreaks.com/topic/206183-questions-on-preg_match/#findComment-1078733 Share on other sites More sharing options...
RopeADope Posted June 29, 2010 Author Share Posted June 29, 2010 @salath -So could I modify the expression to simply be {^A-Za-z0-9} and forgo the quotes all together, or would it have to be "{^A-Za-z0-9}" ? @cags: -That function does exactly what I was looking for, lol. The overwhelming consensus from the search results I've seen is to use preg_match since ereg is deprecated. So ctype_alnum() would return false if any non-alphanumeric characters or a blank was detected? Quote Link to comment https://forums.phpfreaks.com/topic/206183-questions-on-preg_match/#findComment-1078768 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.