Home arrow PHP arrow Page 2 - Using Filters in PHP 5

Filters available in PHP 5 - PHP

You've probably built custom functions and libraries to validate user input in PHP 5. There's an easier way to do this, and it can save you considerable time and effort. It involves using PHP 5's own built-in and often-neglected filters. This nine-part series will introduce you to their use.

TABLE OF CONTENTS:
  1. Using Filters in PHP 5
  2. Filters available in PHP 5
  3. Checking for integer values with the filter PHP extension
  4. Using the FILTER_VALIDATE_INT filter to check for minimal and maximal values
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
July 15, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I start explaining how to use filters in PHP 5, it’s convenient to check which of them comes with the PHP distribution. To do this, I’m going to use a pair of handy native functions called “filter_list()” and “filter_id()." The first one will return an array of the existing filters and the second one will retrieve their corresponding IDs.

A simple demonstration of how to use these functions is coded below. Take a look at the following example:

<?php

 

// example on using the 'filter_list()' function

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

{

echo '<p> Filter Name: ' . $filter . ' ------------- Filter ID: ' . filter_id($filter) . '</p><hr />';

}

 

/* displays the following

 

Filter Name: int ------------- Filter ID: 257

 

Filter Name: boolean ------------- Filter ID: 258

 

Filter Name: float ------------- Filter ID: 259

 

Filter Name: validate_regexp ------------- Filter ID: 272

 

Filter Name: validate_url ------------- Filter ID: 273

 

Filter Name: validate_email ------------- Filter ID: 274

 

Filter Name: validate_ip ------------- Filter ID: 275

 

Filter Name: string ------------- Filter ID: 513

 

Filter Name: stripped ------------- Filter ID: 513

 

Filter Name: encoded ------------- Filter ID: 514

 

Filter Name: special_chars ------------- Filter ID: 515

 

Filter Name: unsafe_raw ------------- Filter ID: 516

 

Filter Name: email ------------- Filter ID: 517

 

Filter Name: url ------------- Filter ID: 518

 

Filter Name: number_int ------------- Filter ID: 519

 

Filter Name: number_float ------------- Filter ID: 520

 

Filter Name: magic_quotes ------------- Filter ID: 521

 

Filter Name: callback ------------- Filter ID: 1024

*/

?>

As you can see in the above code sample, the combined use of the “filter_list()” and “filter_id()” functions provides useful information about the filters that are available with the PHP 5 distribution. In this particular case, the name and ID of each filter is displayed on screen via a simple “foreach” construct, which helps you understand each one's functionality. 

Of course, from the previous example it’s easy to see that there are filters available for checking integers and float numbers, Boolean values and regular expressions, URLs, email and IP addresses, strings and special characters, and so forth. However, since this will be a progressive process, I’m going to discuss  each filter in detail in successive tutorials of this series.

At this point you should have some idea of how useful PHP 5 filters can really be for validating incoming data. Therefore, it’s time to start an in-depth discussion of the first filter shown by the previous list. It is the one responsible for checking whether a given value is an integer.

This brand new PHP 5 filter will be explored in detail in the next segment. Thus, to learn how to work with it, click on the link below and read the next few lines.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: