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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.