Author Topic: preg_match expression  (Read 403 times)

0 Members and 1 Guest are viewing this topic.

Offline jacko310592Topic starter

  • Enthusiast
  • Posts: 222
    • View Profile
preg_match expression
« on: March 25, 2010, 11:51:09 AM »
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

Offline jacko310592Topic starter

  • Enthusiast
  • Posts: 222
    • View Profile
Re: preg_match expression
« Reply #1 on: March 25, 2010, 12:34:30 PM »
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

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: preg_match expression
« Reply #2 on: March 25, 2010, 01:41:12 PM »
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.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline jacko310592Topic starter

  • Enthusiast
  • Posts: 222
    • View Profile
Re: preg_match expression
« Reply #3 on: March 25, 2010, 01:46:02 PM »
thanks cags, that cleared everything up nicely (: