...hmh... I get a parse error... seems like your delimiters aren't correct? Anyway, you're right, validating strings is done very efficiently by Regular Expressions. Though your example RegExp is not correct, it would validate a string on the first occurence of a letter or number.
PHP Code:
$teststring = "foo23foo";
if ( preg_match("/^[a-zA-Z0-9]+$/", $teststring) == 1 ) {
echo 'first method succeeded<br>';
}
if ( preg_match("/[^a-zA-Z0-9]/", $teststring) == 0 ) {
echo 'second method one succeeded';
}
You have two possiblities: Either test if the whole string consists from the start to the end only of [a-zA-Z0-9], as done in the first if-statement, or check for the occurrence of a character that is not included in [a-zA-Z0-9], as done in the second if-statement.
With the first method, your string is ok when you have a match, with the second, the string is only valid when no matches (of forbidden characters) were found, hence you test against 0.
I'd prefer the first RegExp, since it also makes sure that the string isn't empy, which would pass the second method.
hth
mordred