Php filter_var() 是一个函数,用于使用指定的过滤器过滤给定的变量。为了清理和验证诸如 email_id、IP 地址等数据,在 Php 中使用了 filter_var() 函数(其中包含数据)。文本中的验证是指输入的数据格式是否正确。例如,在该人的电子邮件 ID 中,是否存在“@”符号。在电话号码字段中,应包含所有数字或数字。清理意味着清理输入的数据或从中删除非法或不必要的字符,以防止将来出现任何问题。例如,从用户电子邮件中删除不必要的符号和字符。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
下面给出的是 Php 中 filter_var() 函数的基本语法:
filter_var(variable, filtername, options)
哪里,
返回值:上面的函数返回过滤后的值,如果数据/变量没有被过滤,则返回 false。
在 PHP 中,filter_var() 方法接受上述解释的各种参数并返回经过验证/清理的数据。验证是指检查程序员指定的数据格式,清理是指从数据中删除不必要的字符以返回程序员要求的数据。
让我们通过示例了解 Php 中的 filter_var() 函数的工作原理:
使用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>
输出:
说明:
在上面的代码中,要验证的整数值存储在变量“value”中,然后与“FILTER_VALIDATE_INT”过滤器名称一起传递到filter_var()方法中以对其进行验证。最后,应用条件运算符 if 和 else 来检查条件,并使用“echo”在控制台上打印相应的输出。
使用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>
输出:
说明:
在上面的代码中,使用filter_var()方法验证计算机或任何其他网络设备的IP地址。要验证的 IP 地址存储在变量“ip”中。由于 IP 地址具有特定格式“x.y.z.w”,因此使用 filter_var() 函数中的“FILTER_VALIDATE_IP”对其进行验证。最后,验证传递的 IP 地址,并使用“echo”在控制台上打印相应的输出。
使用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>
输出:
说明:
在上面的代码中,首先对具有特定格式的 URL 地址进行清理,然后使用 filter_var() 方法进行验证。要检查的 URL 存储在变量“check_url”中。为了清理该 url,“FILTER_SANITIZE_URL”作为过滤器名称与该 url 一起传递。清理后,将使用“FILTER_VALIDATE_URL”过滤器名称和 url 来验证 url,并使用“echo”在控制台上打印相应的验证输出。
使用filter_var()函数验证用户的电子邮件地址
代码:
<!DOCTYPE html> <html> <body> <?php // email address to be checked $email_check = "[email 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>
输出:
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.’
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:
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.’
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中文网其他相关文章!