Home arrow PHP arrow Page 4 - Cleaning up Array Elements, POST and GET Requests with Filters in PHP 5

Using callback functions and building arrays of filters - PHP

Welcome to the final episode of a series that shows you how to use filters in PHP 5. Made up of nine parts, these articles show you how to utilize the numerous checking filters that come with the filter library. You can use them to thoroughly validate the incoming data handled by your PHP programs, without having to spend a long time coding custom functions or class methods.

TABLE OF CONTENTS:
  1. Cleaning up Array Elements, POST and GET Requests with Filters in PHP 5
  2. Review: the FILTER_SANITIZE_STRING filter
  3. Sanitizing strings from GET and Post requests, cookies and sessions
  4. Using callback functions and building arrays of filters
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 09, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Any introduction to working with the PHP 5 filter extension wouldn’t be complete if it didn't mention the capabilities for using filters in conjunction with callback functions and for building arrays of filters. Yes, the filter library permits you to do this in a simple manner, as you'll see from the couple of examples listed below. Pay close attention to them:

// example on using the FILTER_CALLBACK filter with user defined function

function nl2par($str)

{

return str_replace("n",'<p>', $str);

}

$string = "This is a linenThis is another line";

echo filter_var($string, FILTER_CALLBACK, array('options' => 'nl2par'));

 

 

// example on using the FILTER_CALLBACK filter with PHP function

$string = "This is a linenThis is another line";

echo filter_var($string, FILTER_CALLBACK, array('options' => 'nl2br'));

 

 

// example on using an array of filters

$_POST = array('name' => 'Alejandro Gervasio', 'email' => 'alejandro@domain.com');

// build an array of filters

$filters = array('name' => FILTER_SANITIZE_STRING, 'email' => FILTER_VALIDATE_EMAIL);

// run filters

$filteredValues = filter_var_array($_POST, $filters);

// display filtered values

foreach($filteredValues as $key => $value)

{

echo $key . ' --- ' . $value .'<br />';

}

As you can see above, it’s perfectly feasible to bind different callback functions to a specific filter using the “filter_var()” function. In the first example, this process is performed by using a custom function called “nl2par(),” while in the second case this same task is carried out by means of a native PHP function like “nl2br().”

Finally, there’s an additional code sample that shows how to build an array of filters, which can be applied later on to elements of another input array, such as the superglobal $_POST, $_GET, etc.

And with these examples, I’m concluding this introductory overview on using the filter extension that comes bundled with PHP 5. Of course, you’re completely free to edit and enhance all of the code samples developed in this article series, thus arming yourself with a strong background in working with this powerful validation library.

Final thoughts

Sad but true, we’ve come to the end of this series. Hopefully this long journey has been instructive for you, since you learned how to take advantage of the impressive functionality provided by the PHP 5 filter library. So, now that you’re aware of its existence, the next time that you need to validate incoming data within your PHP applications, you should consider using this extension as a good alternative to coding custom checking functions.

See you in the next PHP tutorial!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: