在PHP5.2中,可以通过内置的扩展过滤器去进行合法性的校验,例子如下:
1 验证是否email:
email = "jason@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "Valid email address!"; }
else
{
echo "Invalid email address!";
}
2 验证是否整数
if (filter_var($age), FILTER_VALIDATE_INT, array('options' => array('min_range' => 13, 'max_range' => 100)))
{
echo "Valid age!";
}
else
{
echo "Invalid age!";
}
3 跟正则表达式
if (filter_var($age), FILTER_VALIDATE_INT, array('options' => array('min_range' => 13, 'max_range' => 100)))
{
echo "Valid age!";
}
else
{
echo "Invalid age!";
}