Check whether the value is float or not
Example
======
<?php
$var=3.67;
var_dump(filter_var($var, FILTER_VALIDATE_FLOAT));
?>
Output
=====
float(3.67)
Check whether the value is float or not
Example
======
<?php
$var=3.67;
var_dump(filter_var($var, FILTER_VALIDATE_FLOAT));
?>
Output
=====
float(3.67)
Check whether the value is Integer or not
Example
======
<?php
$var=200;
$int_options = array(“options”=>array(“min_val”=>10, “max_val”=>356));
var_dump(filter_var($var, FILTER_VALIDATE_INT, $int_options));
?>
Output
=====
int(200)
Check whether the email is valid or not
Example
======
<?php
if(filter_var(“info@phpcodez.com”, FILTER_VALIDATE_EMAIL)) {
echo “Valid Email”;
}else {
echo “Invalid Email”;
}
?>
Output
=====
Valid Email
Check whether the given IP is valid or not
Example
======
<?php
if(filter_var(“345.454.3.3”, FILTER_VALIDATE_IP)) {
echo “Valid IP”;
}else{
echo “invalid IP”;
}
?>
Output
=====
invalid IP
print all filter as an array
General Format
=============
filter_list()
Example
======
<?php
echo “<pre>”;
print_r(filter_list());
?>
Output
=====
Array
(
[0] => int
[1] => boolean
[2] => float
[3] => validate_regexp
[4] => validate_url
[5] => validate_email
[6] => validate_ip
[7] => string
[8] => stripped
[9] => encoded
[10] => special_chars
[11] => full_special_chars
[12] => unsafe_raw
[13] => email
[14] => url
[15] => number_int
[16] => number_float
[17] => magic_quotes
[18] => callback
)
Read more than one input from the outside script and filter all
General Format
=============
filter_input_array(input_type, filter_args)
input_type – A valid input type
*) INPUT_COOKIE
*) INPUT_GET
*) INPUT_ENV
*) INPUT_POST
*) INPUT_SERVER
filter_args – Array filter arguments
Example
=======
<?php
$filters = array( “phone” => FILTER_VALIDATE_INT, “url”=> FILTER_VALIDATE_URL, );
print_r(filter_input_array(INPUT_POST, $filters));
?>
Diaplay the ID of a given filter
General Format
==============
filter_id(filterName)
filterName – filter name to get the id of it
Example
=======
<?php
echo(filter_id(“validate_url”));
?>
Output
=====
273
Filter multiple variable after reading them
General Format
==============
filter_var_array(array, args)
array – Array of values to be filtered
args – array of filter arguments
Example
=======
<?php
$array = array ( “zipcode” => “453441s”, “email” => “info@phpcodez.com”, );
$filters = array ( “zipcode” => FILTER_VALIDATE_INT, “email”=> FILTER_VALIDATE_EMAIL, );
print_r(filter_var_array($array, $filters));
?>
Output
=====
Array ( [zipcode] => [email] => info@phpcodez.com )
Read the input from the outside script and filter the same
Genaral Format
filter_input(input_type, variable, filter)
Input_type – a valid input ype
*) INPUT_COOKIE
*) INPUT_GET
*) INPUT_ENV
*) INPUT_POST
*) INPUT_SERVER
variable – Value to be filtered
filter – A valid ID
Example
<?php
if (filter_input(INPUT_POST, ‘url’, FILTER_VALIDATE_URL)) {
echo “Valid URL”;
}else {
echo “Invalid URL”;
}
?>
It can be used to check whether a variable of specified input type exists
General Format
filter_has_var(type, variable)
type – Possible types and can be one of the followings
*) INPUT_COOKIE
*) INPUT_GET
*) INPUT_ENV
*) INPUT_POST
*) INPUT_SERVER
variable – hold the value to be checked
Example
=======
<?php
if(!filter_has_var(INPUT_GET, “email”)){
echo(“Does not exist”);
}else {
echo(“Exists”);
}
?>