My function is not passing true. I'm not sure if I have my expressions correct. I'm trying to limit the $value to one alphanumberics. function check_special_chars($value,$type) { if($type==1) { $pattern="[a-zA-Z0-9]"; } //alphanumeric with caps elseif($type==2) { $pattern="[a-zA-Z0-9_-]"; } //alphanumeric with underscore and dash elseif($type==3) { $pattern="[0-9]"; } //0 to 9 only elseif($type==4) { $pattern="[a-z]"; } //alpha lowercase only if(preg_match($pattern,$value)) { return 1; } else { return 0; } } PHP:
Not really an answer to your problem, but this tool is extremely helpful: http://weitz.de/regex-coach/
function check_special_chars($value,$type) { if($type==1) { $pattern="/^[a-zA-Z0-9]*$/"; } //alphanumeric with caps elseif($type==2) { $pattern="/^[a-zA-Z0-9_-]*$/"; } //alphanumeric with underscore and dash elseif($type==3) { $pattern="/^[0-9]*$/"; } //0 to 9 only elseif($type==4) { $pattern="/^[a-z]*$/"; } //alpha lowercase only if(preg_match($pattern,$value)) { return 1; } else { return 0; } } PHP: Maybe you should check Ctype functions and the switch statement.
can you give some examples what you have an what you want out of this function? afaik there is a regex expression covering all the special chars, similar to 0-9 for numbers. check out http://en.wikipedia.org/wiki/Regex and scroll down to the table with posix classes, maybe this helps. otherwise ask nintendo, he knows it best and will be happy to help
Ya, it'd help to know what you're trying to match. But to add to SoKickIt's example, you need to escape the dashes as well ( \- ). And unless you want to match no characters, I'd change the star (*) to a plus (+) so it'll match 1 or more of the given characters.