It can be used to call a user defined function to filter the value .
Example
======
<?php
function removeSpace($var) {
return str_replace(” “, “”, $var);
}
echo filter_var(“PHP Codez”, FILTER_CALLBACK,array(“options”=>”removeSpace”));
?>
Output
=====
PHPCodez
It Escapes special characters like &,< etc
Example
======
<?php
print_r(filter_var(“PHPCodez & Me”,FILTER_SANITIZE_SPECIAL_CHARS));
?>
Output
=====
PHPCodez & Me (If you take view source , it will be PHPCodez & Me)
Check whether the url is valid or not
Example
======
<?php
if(filter_var(“http://phpcodez.com”, FILTER_VALIDATE_URL)) {
echo “Valid URL”;
}else {
echo “Invalid URL”;
}
?>
Output
=====
Valid URL
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
Zend | Magento Certified PHP | eCommerce Architect