Jump to content

questions on preg_match


RopeADope

Recommended Posts

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.?

Link to comment
https://forums.phpfreaks.com/topic/206183-questions-on-preg_match/
Share on other sites

@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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.