In the previous article, I brought you "PHP Form Learning: The Use and Differences of $_GET and $_POST Variables", which detailed the differences between $_GET variables in PHP and Knowledge about the $_POST variable. In this article, we will take a look at the knowledge about filters in PHP. I hope it can help you!
In the previous article we learned about $_GET
variables and $_POST
variables, which were mentioned Regarding security issues, the PHP filters discussed in this article are used to verify and filter data from non-secure sources, such as user input. Next, let’s take a look at the relevant knowledge of filters in PHP. Let’s take a look.
PHP filter
What is a filter, you can first simply understand the filter as Filter out unsafe data. So why do we use Weiwei? In our daily development, almost all web applications rely on external input. These data usually come from other applications like web services or from users. Through the use of filters we can ensure that the application gets the correct input type.
We should filter external data like input data from forms, cookies, server variables, database query results, etc. It is important to filter input, so we need to use filters .
PHP filters are used to validate and filter data from non-secure sources. They are an important part of any web application when testing, validating and filtering user input or custom data. It is designed to It makes data processing easier and faster.
Functions and filters
When we need to filter variables, we can use many filter functions: filter_var( )
Filter a single variable through a specified filter; filter_var_array()
Filter multiple variables through the same or different filters; filter_input
Get an input variable , and filter it;filter_input_array
Get multiple input variables and filter them through the same or different filters.
Next, let’s take an example to verify an integer through the filter_var()
function. The example is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $int = 123; if(!filter_var($int, FILTER_VALIDATE_INT)) { echo("不是一个合法的整数"); } else { echo("是个合法的整数"); } ?>
Output result:
The above example verifies an integer through the filter_var() filter function. Next, let’s take a look at the two commonly used filters.
<strong>Validating</strong>
Filter: Used to validate user input, with strict format rules (such as URL or E -Mail validation), returning the expected type if successful, or FALSE if failed.<strong>Sanitizing</strong>
Filter: used to allow or prohibit specified characters in the string, no data format rules , always returns a string.
Options and flags
Options and flags are used to add additional filtering to the specified filter options. Different filters have different options and flags.
Next let's look at an example using filter_var()
and "min_range"
and "max_range"
options to verify an integer, The example is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $var=300; $int_options = array( "options"=>array ( "min_range"=>0, //最小值 "max_range"=>256 //最大值 ) ); if(!filter_var($var, FILTER_VALIDATE_INT, $int_options)) { echo("不是一个合法的整数"); } else { echo("是个合法的整数"); } ?>
Output result:
In the above example, it is important to note that: just like the above code, the options must into a related array called "options". If using flags, they don't need to be in an array. Since the integer is "300", it is not within the specified range, so the output is as above.
Validating input
Next let’s try to validate the input from the form. The first thing we need to do is confirm that the input data we are looking for exists. Then we use the filter_input()
function to filter the input data.
Next, let’s take an example to see how the input variable "email" is passed to the PHP page using GET. The example is as follows:
<?php header("Content-type:text/html;charset=utf-8"); if(!filter_has_var(INPUT_GET, "email")) { echo("没有 email 参数"); } else { if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) { echo "不是一个合法的 E-Mail"; } else { echo "是一个合法的 E-Mail"; } } ?>
Output result:
What we need to pay attention to is: the above example has an input variable (email) transmitted through the "GET" method. Check whether there is an "email" input variable of the "GET" type. If there is an input variable , to check whether it is a valid e-mail address.
净化输入
让我们试着清理一下从表单传来的 URL。首先,我们要确认是否存在我们正在查找的输入数据。然后,我们用 filter_input()
函数来净化输入数据。
下面我们通过示例来看一下输入变量 "url" 被传到 PHP 页面,示例如下:
<?php header("Content-type:text/html;charset=utf-8"); if(!filter_has_var(INPUT_GET, "url")) { echo("没有 url 参数"); } else { $url = filter_input(INPUT_GET, "url", FILTER_SANITIZE_URL); echo $url; } ?>
输出结果:
其中我们需要注意的是:
FILTER_SANITIZE_URL 过滤器删除字符串中所有非法的 URL 字符。上面的实例有一个通过 "GET" 方法传送的输入变量 (url):检测是否存在 "GET" 类型的 "url" 输入变量,如果存在此输入变量,对其进行净化(删除非法字符),并将其存储在 $url 变量中。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of Take you through PHP filters in three minutes (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools