flower in dallas

The php filters are used to validate and sanitize the unidentified user input. Use filter_list() function to list the php filter extensions.

PHP filter_var() Function

The filter_var() function is used to validate and sanitize data.

Example:

[php]<?php
$str = "<h1>Hello World!</h1>";
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>[/php]

Code Explanation:

In the above code we have declared the variable $str and provided the value

Hello World!

.

On the next php line, we have created a new variable called $newstr and created a filter_var() function. The filter function we are calling $str and using the filter statement FILTER_SANITIZE_STRING. This statement removes the HTML code from the string. Then we echo the $newstr variable to get the result.

The above code generates this result.

Hello World!

Validate an Integer

Use filter_var() function to check if the value is integer or not.

Example Code:

[php]<?php
$int = 100;

if (!filter_var($int, FILTER_VALIDATE_INT) === false) {
    echo("Integer is valid");
} else {
    echo("Integer is not valid");
}
?>[/php]

Code Explanation:

In the above code, we created a variable $int and provided value 100. Then by declaring the if statement we are using filter statement to check the variable is an integer or not by using statement called FILTER_VALIDATE_INT. If the value is not an integer and not identical then it will show the false result.

In the above code, the integer is valid so the outcome will look like this.

Integer is valid

Sanitize and Validate an Email Address

In this example, the filter_var() function removes the illegal details from the email id and returns correct email.

Example Code:

[php]<?php
$email = "john.doe@//((example.com";

// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
    echo("$email is a valid email address");
} else {
    echo("$email is not a valid email address");
}
?>[/php]

Code Example:

The above code we have intentionally added to additional symbols in the email id. Our email id looks like this “john.doe@//((example.com” which indicates the wrong format of using email id.

By using the filter_var() function and the FILTER_VALIDATE_EMAIL statement we can remove the illegal characters from the id.

The above code will show the result.

john.doe@example.com is a valid email address

The filter function has removed the illegal symbols from the email id.

Validate the URLs

Similarly you can use the use the filter function to validate the URLs.

Example

[php]<?php
$url = "https://www.w3schools.com";

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

// Validate url
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
    echo("$url is a valid URL");
} else {
    echo("$url is not a valid URL");
}
?>[/php]

The above will show the result as below.

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

Related Posts

Leave a Reply