How can i know some charac开发者_开发技巧ters are in the string or not? i have 5 invalid characters for example that can not use in the username. {-()*#} how can i do this?
If you just want to check if every single character is a letter or a number you coud use:
if(ctype_alnum($text)){
echo "Success!";
}
if (preg_match('/[-()*#]/', $string)) {
echo 'string contains one or more illegal characters';
}
if (preg_match('/[^a-zA-Z0-9]/', $string)) {
echo 'string contains non-alphanumeric characters';
}
$disallowed = array('-', '(', ')', '*', '#');
$valid = true;
foreach ($disallowed as $char) {
if (strpos($username, $char) !== false) $valid = false;
}
if ($valid)
echo 'valid username';
else
echo 'invalid username';
if (preg_match('/(\{|\(|-|\)|\*|#|\})+/g', $string)) {
echo 'invalid characters';
}
精彩评论