I am using
preg_match('[^a-zA-Z0-9_]', $testvar)
and when $testvar = “hgufy&&^%fff” it returns 0 and when $testvar = “kfjghfu” it returns 0.
What am I doing wrong. I would assume that in the first case it should return a positive result.
I am using
preg_match('[^a-zA-Z0-9_]', $testvar)
and when $testvar = “hgufy&&^%fff” it returns 0 and when $testvar = “kfjghfu” it returns 0.
What am I doing wrong. I would assume that in the first case it should return a positive result.
Go look at the preg_match documentation and you will see that regular expressions are contained inside a pair of forward slashes.
The reason why “hgufy&&^%fff” returns false is because is contains characters other then A-Z, 0-9 and _. The only real problem i see is no // declarations before [ and after ] which are required for [B]preg_match/B unlike [B]ereg/B which doesn’t require this.
Try the following as the + sign will check for repeated expressions within the string.
preg_match('/[^a-zA-Z0-9_]+/', $testvar)
Your’re missing something called “delimiters”. The first character inside a pattern is treated as a delimiter, which in your case is “[”; its corresponding closing delimiter would be “]” and your actual pattern becomes:
^a-zA-Z0-9_
Obviously wrong.
try adding a delimiter, such as “/” – though I prefer “@”:
preg_match('@[^a-zA-Z0-9_]@', $testvar);
You can shortenup your regexp like this:
preg_match('@\\W@', $testvar);
Thanks for the input, works great now. And Paul, I did look at the documentation several times and just missed it I guess.