FILTER_VALIDATE vs Preg_match。哪一个使用? [英] FILTER_VALIDATE vs Preg_match. Which one to use?
问题描述
为了验证输入日期,无论是从表单URL还是从表单中,您通常使用哪种技术? 我一直在寻找 PHP过滤器,但是我很少看到它们在任何代码中。
$ numbers =/ ^ [0-9] + $ / d;
if(preg_match($ numbers,$ var)){
echois number;
$ b 而不是:
$ b
if(!filter_var($ var,FILTER_VALIDATE_INT))
echois number;
$ / code $ / pre
使用这个或另一个有什么优势吗?
谢谢!
解决方案 PHP自php 5.2以来引入了多种类型的变量验证的filer_功能。使用 filter_var 是个好习惯 filter_input 满足您的需求。
除了更安全外,还可以节省大量开发时间。
To validate input date, both form URL or from a form, which technique do you usually use?
I have been looking at PHP Filters but i have rarely see them on any code.
I have usually seen the use of preg_mach, for example:
$numbers = "/^[0-9]+$/D";
if (preg_match($numbers, $var)){
echo "is number";
}
Instead of:
if(!filter_var($var, FILTER_VALIDATE_INT))
echo "is number";
}
Is there any advantage using one or another?
Thanks!
解决方案 Php introduced the filer_ functionality for a wide range of types of variable validation since php 5.2. It's good practice to use the filter_var || filter_input for your needs.
Besides the fact that it's safer it will also save you lots of development time cracking your head over regular expressions.
这篇关于FILTER_VALIDATE vs Preg_match。哪一个使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!