Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting illegal chars with preg_match

Status
Not open for further replies.

Sen7inel

IS-IT--Management
Feb 14, 2001
90
FI
Hi,

I'm using

preg_match("/[^a-zA-Z0-9.@_\-]/", $str)

to detect illegal or possibly "dangerous" characters from input. It works all right, but just out of curiosity, why doesn't this just evaluate the first character of $str?
 
I didn't test it, but I'm pretty sure you have to place the ^ before the character class, like this:
Code:
preg_match("/^[a-zA-Z0-9.@_\-]/", $str)
 
preg_match("/^[a-zA-Z0-9.@_\-]$/", $str);

or a shorted version;

preg_match("/^[\w\d.@_\-]$/", $str);

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top