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
https://forums.phpfreaks.com/topic/196503-preg_match-expression/
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

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.

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.