---
title: "PHP Filter"  
description: "PHP filters are used to validate and filter data coming from insecure sources, like user input. A PHP filter is used to validate and filter data comin"  
author: "Anonymous User"  
published: 2011-09-16  
updated: 2019-12-04  
canonical: https://www.mindstick.com/articles/732/php-filter  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# PHP Filter

PHP filters are used to validate and [filter data](https://www.mindstick.com/forum/384/based-on-logged-username-to-filter-data-in-gridview) coming from insecure sources, like [user input](https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input). A PHP filter is used to validate and filter data coming from insecure sources. To test, validate and filter user input or [custom data](https://www.mindstick.com/forum/160248/how-to-create-custom-data-annotations-for-custom-validation-on-model-property-in-dot-net-core) is an important part of any [web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about). The PHP filter extension is designed to make [data filtering](https://www.mindstick.com/forum/158701/how-can-implement-client-side-data-filtering-or-search-functionality-using-jquery) easier and quicker. The PHP filter extension has many of the functions needed for checking many types of user input, handled locally this provides a standard method of [filtering data](https://www.mindstick.com/forum/160900/how-to-use-searching-and-filtering-data-in-an-sql-server). You should always filter all external data.\

External data may be input data from a form, cookies data, server variables, [web service](https://www.mindstick.com/articles/45/web-services-in-asp-dot-net) data and [database query](https://www.mindstick.com/forum/159404/a-database-query-returns-a-syntaxerror) result etc.

**There are three types of filters which are used in PHP, defined as follows.**

1. Validate Filters
2. Sanitize Filter
3. Other Filter

##### Validate Filters:

Validate filters are used to [validate user](https://www.mindstick.com/forum/157975/how-can-use-jquery-s-form-validation-plugin-to-validate-user-input-and-provide-feedback-to-users) input value such as: integer, Boolean, float, [IP address](https://www.mindstick.com/articles/23391/why-you-should-treat-your-ip-address-like-a-closely-guarded-secret), email, URL etc. Validate filter follow the strict format rule such as email, URL etc.

Let’s have an example, how to use validate filter id in PHP.

##### Example:

```
<!DOCTYPE html><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title></title>     </head>     <body>         <?php                    $val =12.0 ;             if (filter_var($val,FILTER_VALIDATE_INT))            {                 print ("valid int ");            }             else            {                 print ("Invalid int");            }         ?>     </body></html>
```

##### Output:

![PHP Filter](https://www.mindstick.com/mindstickarticle/eb601128-30ec-4720-9323-ba6cc25138c0/images/a720a1b0-262c-4b3e-9e9c-901cee11d1ff.png)

In the same manner we can use all the validate filter id such as: FILTER_VALIDATE_INT,FILTER_VALIDATE_EMAIL, FILTER_VALIDATE_FLOAT etc.

##### Sanitize Filter:

Sanitize filter are used to allow or disallow specified characters in a string. Sanitize filter have no format rule, it always return string value.

Let’s have an example, how to use Sanitize filter in PHP.

##### Example:

Here we have ‘SanitizeFilterPage.php’ having ‘submit’ button to submit form onto server.

```
<!DOCTYPE html><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title></title>     </head>     <body>        <form id="frmBody" method="get"action="SanitizeFilter.php">         <table>                       <tr>                 <td>TO:</td>                 <td>     <input type="text" id="txtEmailTo" name="emailTo" value="" ></input></td>             </tr>             <tr>                 <td> From:</td>                 <td> <input type="text" id ="txtEmailFrom" name ="emailFrom" value=""></input></td>             </tr>             <tr>                 <td>Subject:</td>                 <td><input type="text" id="txtSubject" name="emailSubject" value=""></input></td>             </tr>             <tr>                 <td>                    Message Body:                 </td>                 <td>                     <textarea id ="txtaraeMsgBody" name ="areaMsgBody" value=""></textarea>                  </td>             </tr>                     <tr>                 <td>                                     </td>                   <td>                     <input type ="submit" id="btnsubmit" name="btnSubmitEmail" value="Submit" ></input>                 </td>             </tr>                     </table>     </body></html>
```

\

![PHP Filter](https://www.mindstick.com/mindstickarticle/eb601128-30ec-4720-9323-ba6cc25138c0/images/7fb12fe6-690a-4fd3-a438-00108dc38a94.png)

When we click on ‘Submit’ button, the form is submitted to server with Get method, now we can filter URL on the ‘SanitizFilter.php’ page.

```
<!DOCTYPE html><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title></title>     </head>     <body>         <?php                         if(!filter_has_var(INPUT_GET, "emailTo"))            {                 echo 'url does not exit';            }             else            {                $url =  filter_input(INPUT_GET, "emailTo",  FILTER_SANITIZE_URL);                 echo $url ;            }         ?>     </body></html>
```

![PHP Filter](https://www.mindstick.com/mindstickarticle/eb601128-30ec-4720-9323-ba6cc25138c0/images/c81a2cb6-16ef-4d8f-92a2-107636be34d2.png)

Here, we are filtering URL with ‘emailTo’ name.

##### Other Filter:

‘FILTER_CALLBACK’ is used for filter data by calling user defined function. This filter gives us full control over the data filtering.

Let’s have an example, how to use FILTER_CALLBACK in PHP with user defined function.

##### Example:

```
        <?php         // user define functionto filter user input data          function MyCallbackFunction($name)         {              return str_replace(" ",  " _ ", $name);         }           $name = "My name is Arun singh !";           echo filter_var($name,  FILTER_CALLBACK,  array("options"=>"MyCallbackFunction"));      ?>
```

##### Output:

Here blank space (‘ ‘) is replaced by ‘ _ ‘

![PHP Filter](https://www.mindstick.com/mindstickarticle/eb601128-30ec-4720-9323-ba6cc25138c0/images/39ce13df-ebb5-47cc-8138-915b7c8e8542.png)

\

---

Original Source: https://www.mindstick.com/articles/732/php-filter

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
