How To Use PHP Filters Function Example ?

PHP 8 , PHP

Nicesnippets

675

21-06-2021


How To Use PHP Filters Function Example ?

Hi guys,

Today i will explained how to use php filters function in your php file. This example is so easy to use in php. This example to i am use in many filters function in php file.

PHP filters are used to validate and sanitize external input. PHP filter extension many of the functions needed to checking user input, and is designed to make validation easy and fastly.

THe filter function is provided in a php in by default. Filter function use to vary easy here. So let's start to the example.

Filter_list() Function


Filter_list() function used to list of the PHP filter extension.

<?php

<table>

<tr>

<td>Filter Name</td>

<td>Filter ID</td>

</tr>

<?php

foreach (filter_list() as $id => $filter) {

echo '<tr><td>' . $filter . '</td>

<td>' . filter_id($filter) . '</td></tr>';

}

?>

</table>

?>

Output

Filter Name Filter ID

int 257

boolean 258

float 259

validate_regexp 272

validate_domain 277

validate_url 273

validate_email 274

validate_ip 275

validate_mac 276

string 513

stripped 513

encoded 514

special_chars 515

full_special_chars 522

unsafe_raw 516

email 517

url 518

number_int 519

number_float 520

magic_quotes 521

add_slashes 523

callback 1024

FILTER_SANITIZE_STRING

FILTER_SANITIZE_STRING is work to remove to the all html tags in a string.

<?php

$str = "<h1>How Are You!</h1>";

$returnstr = filter_var($str, FILTER_SANITIZE_STRING);

echo $returnstr;

?>

Output

How Are You!

FILTER_VALIDATE_INT

FILTER_VALIDATE_INT is check to the variable value is integer then true otherwise return to the false.

<?php

$int = 100;

if (!filter_var($int, FILTER_VALIDATE_INT) === false) {

echo("Integer is valid");

} else {

echo("Integer is not valid");

}

?>

Output

Integer is valid

Filter_var() And Problem With 0

Variable value is set the 0 then return to the Integer is not valid.

<?php

$int = 0;

if (filter_var($int, FILTER_VALIDATE_INT) === 0 || !filter_var($int, FILTER_VALIDATE_INT) === false) {

echo("Integer is valid");

} else {

echo("Integer is not valid");

}

?>

Output

Integer is valid

FILTER_VALIDATE_IP

FILTER_VALIDATE_IP is use to check the variable ip is valid or not.

<?php

$ip = "127.0.0.1";

if (!filter_var($ip, FILTER_VALIDATE_IP) === false) {

echo("$ip is a valid IP address");

} else {

echo("$ip is not a valid IP address");

}

?>

Output

127.0.0.1 is a valid IP address

FILTER_VALIDATE_EMAIL

FILTER_VALIDATE_EMAIL is use to check the variable email address is valid or not.

<?php

$email = "[email protected]";

$email = filter_var($email, FILTER_SANITIZE_EMAIL);

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {

echo("$email is a valid email address");

} else {

echo("$email is not a valid email address");

}

?>

Output

[email protected] is a valid email address

FILTER_SANITIZE_URL

FILTER_SANITIZE_URL is use to check the variable url is valid or not.

<?php

$url = "https://www.nicesnippets.com";

$url = filter_var($url, FILTER_SANITIZE_URL);

if (!filter_var($url, FILTER_VALIDATE_URL) === false) {

echo("$url is a valid URL");

} else {

echo("$url is not a valid URL");

}

?>

Output

https://www.nicesnippets.com is a valid URL

Now you can check your own.

I hope it can help you...


Recommended Posts