$pattern = "/w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*/";
if (!preg_match($pattern, $email)) {
        throw new Exception(self::ERROR_PARAMETER_EMPTY . '_邮件格式有误:' . $email);
}
登录后复制

如果不用正则还有其它跟简单的方法吗?

使用filter相关函数

filter_has_var

判断$_GET 的结果是否包含name

if(!filter_has_var(INPUT_GET, "name"))
{
    echo("name 不存在");
}
else
{
    echo("name 存在");
}
登录后复制

filter_input

看一个验证邮箱的例子

if (!filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL))
{
    echo "E-Mail is not valid";
}
else
{
    echo "E-Mail is valid";
}
登录后复制

filter_input_array

对整个input源进行过滤

$filters = array
(
    "name" => array
    (
        "filter"=>FILTER_CALLBACK,
        "flags"=>FILTER_FORCE_ARRAY,
        "options"=>"ucwords"
    ),
    "age" => array
    (
        "filter"=>FILTER_VALIDATE_INT,
        "options"=>array
        (
            "min_range"=>1,
            "max_range"=>120
        )
    ),
    "email"=> FILTER_VALIDATE_EMAIL,
);
print_r(filter_input_array(INPUT_POST, $filters));
登录后复制

filter_var,filter_var_array

不需要input源,直接对值进行过滤

if(!filter_var("someone@example....com", FILTER_VALIDATE_EMAIL))
 {
 echo("E-mail is not valid");
 }
else
 {
 echo("E-mail is valid");
 }
登录后复制

input源的范围

验证

其它过滤方法

*提示:如需把特殊的 HTML 实体转换回字符,请使用 htmlspecialcharsdecode() 函数。


$input = "<span>我是标题</span>";
echo htmlspecialchars($input) . "n";
echo htmlentities($input) . "n";
echo strip_tags($input) . "n";

$input = "-- 'select * from ";
echo addslashes($input) . "n";
登录后复制

推荐学习:《PHP视频教程》

以上就是一文详解关于php数据过滤的详细内容,更多请关注悠悠之家其它相关文章!

点赞(28)

评论列表共有 0 条评论

立即
投稿
返回
顶部