如何写只接受数字和字母
function clean_post($_POST) {
foreach($_POST as $key=>$value) {
$cleaned_value = mysql_real_escape_string($value);
$cleaned_value = str_replace('(','', $cleaned_value);
$h_cleaned_array[$key] = $cleaned_value;
}
return $h_cleaned_array;
}
答案 0 :(得分:1)
将此/^[A-Za-z0-9]+$/u
与preg_match
结合使用。
以下是卫生设施的代码 -
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
这用于验证 -
if(preg_match('/[^a-zA-Z0-9]/i', $s))
{
echo "not valid alphanumeric string";
}
根据需要准备一个功能。
答案 1 :(得分:1)
if (!ctype_alnum($value)) {
throw new InvalidArgumentException("'$value' must consist of only alphanumeric characters");
}
答案 2 :(得分:0)
答案 3 :(得分:0)
$pattern = "/[a-z0-9A-Z]+/i";
if(preg_match($pattern,$var){
echo "It's an acceptable value";
}