List of Php filter types

PHP provides a set of built-in filters that can be used to validate and sanitize data.

Here is a list of the most common PHP filter types:

Filter types list

TypeDescription
FILTER_VALIDATE_INTValidates an integer value.
FILTER_VALIDATE_FLOATValidates a floating-point value.
FILTER_VALIDATE_BOOLEANValidates a Boolean value.
FILTER_VALIDATE_EMAILValidates an email address.
FILTER_VALIDATE_URLValidates a URL.
FILTER_VALIDATE_IPValidates an IP address.
FILTER_VALIDATE_REGEXPValidates a value against a regular expression.
FILTER_SANITIZE_STRINGRemoves tags and special characters from a string.
FILTER_SANITIZE_ENCODEDURL-encodes a string.
FILTER_SANITIZE_SPECIAL_CHARSConverts special characters to HTML entities.
FILTER_SANITIZE_EMAILRemoves illegal characters from an email address.
FILTER_SANITIZE_URLRemoves illegal characters from a URL.
FILTER_SANITIZE_NUMBER_INTRemoves illegal characters from an integer value.
FILTER_SANITIZE_NUMBER_FLOATRemoves illegal characters from a floating-point value.
FILTER_SANITIZE_MAGIC_QUOTESApplies addslashes() to a value.


These filters can be used with the filter_var() function, which takes two parameters: the value to be filtered and the filter type.

Example

<?php
$website = "https://www.example.com";
if (filter_var($website, FILTER_VALIDATE_URL)) {
    echo "URL is valid";
} else {
    echo "URL is not valid";
}
?>


Examples by Filter types

All Php filters examples.

FILTER_VALIDATE_INT

<?php
$age = "25";
if (filter_var($age, FILTER_VALIDATE_INT)) {
    echo "Age is valid";
} else {
    echo "Age is not valid";
}
?>


FILTER_VALIDATE_FLOAT

<?php
$price = "19.99";
if (filter_var($price, FILTER_VALIDATE_FLOAT)) {
    echo "Price is valid";
} else {
    echo "Price is not valid";
}
?>


FILTER_VALIDATE_BOOLEAN

<?php
$bool = "true";
if (filter_var($bool, FILTER_VALIDATE_BOOLEAN)) {
    echo "Boolean is valid";
} else {
    echo "Boolean is not valid";
}
?>


FILTER_VALIDATE_EMAIL

<?php
$email = "john.doe@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Email is valid";
} else {
    echo "Email is not valid";
}
?>


FILTER_VALIDATE_URL

<?php
$website = "https://www.example.com";
if (filter_var($website, FILTER_VALIDATE_URL)) {
    echo "URL is valid";
} else {
    echo "URL is not valid";
}
?>


FILTER_VALIDATE_IP

<?php
$ip = "127.0.0.1";
if (filter_var($ip, FILTER_VALIDATE_IP)){
    echo "IP is valid";
} else {
    echo "IP is not valid";
}
?>


FILTER_VALIDATE_REGEXP

<?php
$string = "abc123";
if (filter_var($string, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-z]+\d+$/")))) {
    echo "String is valid";
} else {
    echo "String is not valid";
}
?>


FILTER_SANITIZE_STRING

<?php
$string = "<p>Hello, <b>world</b>!</p>";
$string = filter_var($string, FILTER_SANITIZE_STRING);
echo $string; // Outputs: "Hello, world!"
?>


FILTER_SANITIZE_ENCODED

<?php
$string = "Hello, world!";
$string = filter_var($string, FILTER_SANITIZE_ENCODED);
echo $string; // Outputs: "Hello%2C+world%21"
?>


FILTER_SANITIZE_SPECIAL_CHARS

<?php
$string = "Hello, <b>world</b>!";
$string = filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS);
echo $string; // Outputs: "Hello, &lt;b&gt;world&lt;/b&gt;!"
?>


FILTER_SANITIZE_EMAIL

<?php
$email = "john.doe@example.com";
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
echo $email; // Outputs: "john.doe@example.com"
?>


FILTER_SANITIZE_URL

<?php
$url = "https://www.example.com?param=value";
$url = filter_var($url, FILTER_SANITIZE_URL);
echo $url; // Outputs: "https://www.example.com?param=value"
?>


FILTER_SANITIZE_NUMBER_INT

<?php
$number = "25";
$number = filter_var($number, FILTER_SANITIZE_NUMBER_INT);
echo $number; // Outputs: 25
?>


FILTER_SANITIZE_NUMBER_FLOAT

<?php
$number = "19.99";
$number = filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT);
echo $number; // Outputs: 19.99
?>


FILTER_SANITIZE_MAGIC_QUOTES

<?php
$string = "Hello, 'world'!";
$string = filter_var($string, FILTER_SANITIZE_MAGIC_QUOTES);
echo $string; // Outputs: "Hello, \'world\'!"
?>

These filters can be used to validate and sanitize user input, like form data, to ensure that it is safe to use in your application. Keep in mind that using multiple filters and validation methods is a good practice for securing your application.

kabeer

Added  Jan 26, 02:09 am

Article tags

Php
×
SignUp with Email
X