PHP 过滤器_var

WBOY
发布: 2024-08-29 13:03:59
原创
961 人浏览过

Php filter_var() 是一个函数,用于使用指定的过滤器过滤给定的变量。为了清理和验证诸如 email_id、IP 地址等数据,在 Php 中使用了 filter_var() 函数(其中包含数据)。文本中的验证是指输入的数据格式是否正确。例如,在该人的电子邮件 ID 中,是否存在“@”符号。在电话号码字段中,应包含所有数字或数字。清理意味着清理输入的数据或从中删除非法或不必要的字符,以防止将来出现任何问题。例如,从用户电子邮件中删除不必要的符号和字符。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

下面给出的是 Php 中 filter_var() 函数的基本语法:

filter_var(variable, filtername, options)
登录后复制

哪里,

  • 变量:该参数代表变量字段,即需要过滤的变量。这是必填字段。
  • filtername: 该参数代表用户想要使用的过滤器的名称。它是一个可选参数。如果未指定,则使用 FILTER_DEFAULT,这意味着不对给定变量进行过滤。
  • 选项:此参数是可选的。它指定要使用的选项/标志。它基本上是标志或选项的按位析取的关联数组。如果在filter_var()函数中使用此参数,则必须在“flags”字段中提供一个标志,并且必须为回调函数传递一个可调用类型。接受所有参数后,返回经过过滤和清理的变量。

返回值:上面的函数返回过滤后的值,如果数据/变量没有被过滤,则返回 false。

Php 中的filter_var 函数如何工作?

在 PHP 中,filter_var() 方法接受上述解释的各种参数并返回经过验证/清理的数据。验证是指检查程序员指定的数据格式,清理是指从数据中删除不必要的字符以返回程序员要求的数据。

PHP filter_var 示例

让我们通过示例了解 Php 中的 filter_var() 函数的工作原理:

示例#1

使用filter_var()函数验证整数值:

代码:

<!DOCTYPE html>
<html>
<body>
<?php
// Integer value to check
$value = 789787;
// passing the value in the filter_var() function
if (filter_var($value, FILTER_VALIDATE_INT))
{
echo("Congratulations!!! $value is a valid integer value");
}
else
{
echo("Sorry!! $value is not a valid integer value");
}
?>
</body>
</html>
登录后复制

输出:

PHP 过滤器_var

说明:

在上面的代码中,要验证的整数值存储在变量“value”中,然后与“FILTER_VALIDATE_INT”过滤器名称一起传递到filter_var()方法中以对其进行验证。最后,应用条件运算符 if 和 else 来检查条件,并使用“echo”在控制台上打印相应的输出。

示例#2

使用filter_var()函数验证计算机设备的IP地址

代码:

<!DOCTYPE html>
<html>
<body>
<?php
// Ip Address to validate
$ip = '180.0.0';
//Passing the ip address and applying the specific ip filter name
if (filter_var($ip, FILTER_VALIDATE_IP)){
echo("Congratulations!! $ip is a valid IP address, passed by the you");
}
else
{
echo("Sorry $ip is an incorrect IP address");
}
?>
</body>
</html>
登录后复制

输出:

PHP 过滤器_var

说明:

在上面的代码中,使用filter_var()方法验证计算机或任何其他网络设备的IP地址。要验证的 IP 地址存储在变量“ip”中。由于 IP 地址具有特定格式“x.y.z.w”,因此使用 filter_var() 函数中的“FILTER_VALIDATE_IP”对其进行验证。最后,验证传递的 IP 地址,并使用“echo”在控制台上打印相应的输出。

示例 #3

使用filter_var()函数清理和验证URL地址

代码:

<!DOCTYPE html>
<html>
<body>
<?php
// URL which is to be checked
$check_url = "https::////www.abc.com//";
// Sanitizing the URL by removing unnecessary characters from it if any
$check_url = filter_var($check_url, FILTER_SANITIZE_URL);
// Validating the url by passing the appropriate filter name and the sanitized url
if(!filter_var($check_url, FILTER_VALIDATE_URL) == false) {
echo("Congratulations!!! $check_url is the correct URL");
}
else
{
echo("Sorry!! $check_url is an invalid URL");
}
?>
</body>
</html>
登录后复制

输出:

PHP 过滤器_var

说明:

在上面的代码中,首先对具有特定格式的 URL 地址进行清理,然后使用 filter_var() 方法进行验证。要检查的 URL 存储在变量“check_url”中。为了清理该 url,“FILTER_SANITIZE_URL”作为过滤器名称与该 url 一起传递。清理后,将使用“FILTER_VALIDATE_URL”过滤器名称和 url 来验证 url,并使用“echo”在控制台上打印相应的验证输出。

示例#4

使用filter_var()函数验证用户的电子邮件地址

代码:

<!DOCTYPE html>
<html>
<body>
<?php
// email address to be checked
$email_check = "[email&#160;protected]";
// Validating the email by passing the email address and the filtername
if (filter_var($email_check, FILTER_VALIDATE_EMAIL))
{
echo("Congratulations!! $email_check is a valid email address");
}
else
{
echo("Sorry!! You have entered an incorrect email address");
}
?>
</body>
</html>
登录后复制

输出:

PHP 过滤器_var

Explanation:

In the above example, the email address which is to be checked is stored in the variable ‘email_check.’ It is validated using the filter_var() function in Php, bypassing the email variable and the respective filter name (FILTER_VALIDATE_EMAIL). Since the passed email is invalid, so the response is printed on the console using the ‘echo.’

Example #5

Code:

<!DOCTYPE html>
<html>
<?php
// Integer value to be checked
$value = 465675;
// Validating the above integer value range using the 'options' parameter
if(filter_var($value, FILTER_VALIDATE_INT, array("options" => array("min_range" => 10,"max_range" => 4000))))
{
echo "Integer $value is within the specified range";
}
else
{
echo "Sorry!! Integer $value is not in the range provided by you";
}
?>
</body>
</html>
登录后复制

Output:

PHP 过滤器_var

Explanation:

In the above example, the Integer value is to be validated for the given range, i.e., 10 to 400 is tested. Then, in the filter_var() function, the value to be tested is passed along with the filter name (FILTER_VALIDATE_INT) and 1 optional parameter, i.e., ‘options’ having the array with the minimum and maximum range specified. Finally, the variable is validated, and accordingly, the response is printed on the console using the ‘echo.’

Conclusion

The above description clearly explains what is filter_var() functions in Php and how it works to validate and sanitize the variable passed in it. It is one of the important functions that programmers commonly use to filter the data to prevent a security breach. However, this function facilitates the use of different filters by passing the different parameters according to the specific requirements, so the programmer needs to understand it deeply before using it in the program.

以上是PHP 过滤器_var的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!