Jump to content

preg_match expression


jacko310592

Recommended Posts

hey guys, i have to following function which i need to cross-check data with to make sure the data only contains  A-Z a-z 0-9 and _

but at the moment, it seems to let through all symbols

 

function alphanumericDatacheck($data){
return
preg_match ("^[a-zA-Z0-9_]$^", $data); 
}


//--- Useage example...

if (alphanumericDatacheck($username) == false){
$usernameErr = "Can only contain Letters (A-Z), Numbers (0-9) and Underscore (_)";
}

 

can anyone suggest where im going wrong, im really no good with expressions

thanks

Link to comment
Share on other sites

i found the following expression which seems to solve my problem:

 

#^[a-z0-9_]+$#i

 

this allows A-Z a-z 0-9 and _

 

but can anyone explain this to me..

as 'A-Z' isint included in the expression, how does this allow caps?

and what does the  "#^"  and  "+$#i"  parts mean?

 

thanks

Link to comment
Share on other sites

The hashes are delimiters these as the name suggest delimit a pattern, it doesn't have to be the hash character. It also separates the pattern from the modifiers. The i at the end is a modifier and it means make the pattern case insensitive, which is why it picks up capital letters also. The ^ character matches the start of the string and the $ matches the end of the string. The + is a quantifier it means match 1 or more characters from the character class.

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.