Home  >  Article  >  Backend Development  >  What are the php filter functions? Introduction to php filter functions

What are the php filter functions? Introduction to php filter functions

不言
不言forward
2018-10-26 16:50:422214browse

This article brings you what are the PHP filter functions? The introduction of PHP filter function has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

filter_has_var: Detects whether a variable of the specified type exists. Accepts two parameters, the first is the variable type, and the second is the variable name.

filter_id: Returns the id associated with a filter of a specific name.

filter_list: Returns the supported filter list.

filter_input: Get a specific external variable by name and optionally filter it by filter. Accepts four parameters. The first parameter is the variable type, the second parameter is the variable name, and the third parameter is the optional id of the filter to be used. The default is FILTER_DEFAULT, which means no filtering is performed. The fourth parameter is an optional associative array of options to use for the filter, or a bitwise identifier. If the acquisition is successful, it returns the specified variable, if it fails, it returns false, and if the specified variable does not exist, it returns null. If the FILTER_NULL_ON_FAILURE flag is used, false is returned when the variable does not exist, and null is returned if filtering fails.

filter_input_array: Get an array of external variables and optionally filter them by a filter. Accepts four parameters, the first parameter is the variable type, and the second parameter is the optional filter to be used. It can be an array: the key of the array is a string containing the variable name. The value of the array can be a filter type constant, or it can be an array specifying the filter, flag and option. The valid key of this array is specified by filter. Filter type, flags indicates the identifier to be used for the filter or options indicates the options to be used for the filter. It can also be just a filter type constant, and all variables will use this filter. The third parameter is a new optional parameter in PHP5.4, which is used to specify that if the specified key does not exist, its value is set to null, and the default is true. If the processing is successful, an array containing all variables is returned, otherwise false is returned. Returns null if the specified variable does not exist. If the FILTER_NULL_ON_FAILURE flag is used, false is returned when the variable does not exist, and null is returned if filtering fails.

filter_var: Filter a variable using a specific filter. Accepts three parameters, the first is a variable with filtering, the second parameter is the optional id of the filter to be used, the default is FILTER_DEFAULT, that is, no filtering is performed. The third parameter is an associative array of optional filter options, or a bitwise identifier. If the filter accepts options, these flags can be specified via the flags array. For callback-type filters, the name of the callback function should be passed in. The callback function must accept a value to be filtered and return a filtered value.

filter_var_array: Get multiple variables and optionally filter them by filter. It accepts three parameters, the first is a variable array, the key is the variable name, and the value is the variable value, which is the value to be filtered. The second parameter is an optional filter to use. Can be an array: the key of the array is a string containing the variable name. The value of the array can be a filter type constant, or an array specifying the filter, flag and option. The valid key of this array is specified by filter. Filter type, flags indicates the identifier to be used for the filter or options indicates the options to be used for the filter. It can also be just a filter type constant, and all variables will use this filter. The third parameter is a new optional parameter in PHP5.4, which is used to specify that if the specified key does not exist, its value is set to null, and the default is true. If the processing is successful, an array containing all variables is returned, otherwise false is returned. Returns null if the specified variable does not exist. If the FILTER_NULL_ON_FAILURE flag is used, false is returned when the variable does not exist, and null is returned if filtering fails.

<?php
header("Content-type: text/html; charset=utf-8");
$char_br="<br/>";
$str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$num=rand(5,20);
$random_test_value=substr(str_shuffle($str),rand(5,strlen($str)),$num);
$args=array(
	"id" =>array(&#39;filter&#39; => FILTER_VALIDATE_INT,
				 &#39;options&#39;=> array(&#39;min_range&#39; => 1, &#39;max_range&#39; => 10)
		   ),
	"missing"=>FILTER_SANITIZE_ENCODED,
	"searched"=>FILTER_SANITIZE_ENCODED
);
$vars=filter_input_array(INPUT_GET,$args);

echo "<a href=&#39;?searched=$random_test_value&id=$num&#39;>Search again.</a>$char_br";

if(filter_has_var(INPUT_GET,"searched")){
	$search_html = $vars["searched"];
	echo "You have searched for $search_html.$char_br";
}else{
	echo "You searched nothing$char_br";
}
var_dump($vars);
$options=array(
	&#39;options&#39;=>array(
		&#39;default&#39; => 5,
		&#39;min_range&#39; => 1,
		&#39;max_range&#39; => 10
	),
	&#39;flags&#39; => FILTER_FLAG_ALLOW_OCTAL,
);
echo "id filtered by filter_var is:".filter_var($vars["id"],FILTER_VALIDATE_INT,$options)."$char_br";
echo "invoke filter_var_array filter agagin:$char_br";
var_dump(filter_var_array(filter_input_array(INPUT_GET),$args));

echo "支持的过滤器:$char_br id=>name $char_br";
foreach(filter_list() as $v){
	echo filter_id($v),"=>$v$char_br";
}
?>

The above is the detailed content of What are the php filter functions? Introduction to php filter functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete