How to filter user submitted data easily in PHP?

Written by Chris on August 18, 2008 – 7:29 am -

How to filter user submitted data easily in PHP?

Posted using ShareThis

Firstly, as you can see this is my first post made as a direct result of using the ShareThis bookmarklet, which is pretty neat as it actually worked :)

Secondly and more importantly, I wanted to flag this up on my blog as it's something that quite often gets missed in PHP which is actually a very powerful tool.

As the author of the post above mentions, array_map() can be a useful function when sanitizing user data, but it has so many more uses too when dealing with the transformation of a data-set.

Basically a call such as $new_data = array_map('process_data', $old_data); will allow you to transform each element in the $old_data array to a new element in the $new_data array via the function called process_data.

What's more you can manipulate multiple data-sets in this way too by specifying multiple arrays, so long as process_data() can take in the arguments.

For example lets say we have process_data($item1, $item2, $item3) which manipulates $item1, $item2, $item3 to produce a single result.

If we need to perform this calculation on a bulk set of data we can do

$new_data = array_map('process_data', $array_of_item1, $array_of_item2, $array_of_item3);

Easy huh?

I still see people performing these transformations, calculations, whatever, using for, foreach and while loops which can be prone to failure under certain conditions, and are probably less efficient code than simply making this call.

So why don't more people use it?  I don't know, but maybe this post will help raise awareness!



Popularity: 67% [?]

Sphere: Related Content

Related posts on coderchris.com:

  1. Wordpress 2.5 Released!
    Version 2.5 of wordpress has been released and we have a nice new admin interface...
  2. Advanced Syntax-Hilighting Online Code Editors - A Wordpress IDE?
    When I write plugins for wordpress I like to do it on a test...
  3. Simple Function To Force Download Of A File
    The following function will allow you to run a file download through PHP, you...
  4. Refactor My Code - I Wish I’d Thought of It!
    refactormycode.com is a fledgling website setup as a project by French Canadian  Ruby Developer...
  5. Starting App’s For Facebook
    Yesterday for a bit of fun I decided I'd try and write my first...


Tags: , , , , , ,
Posted in PHP, Web Development | Trackback |

One Comment to “How to filter user submitted data easily in PHP?”

  1. Jasper Says:

    I personally don’t use array map because it’s all well and good when you’re expecting only strings to come through for filtering, but if you’re expecting integers to make up dates and the like, using array map in my experience has always resulted in going back and fixing stuff, or having to do more filtering anyway so it’s a bit of a waste of time.

    Maybe I just haven’t thought about it enough.

Leave a Comment

RSS

  • Translator

    English flagChinese (Simplified) flagChinese (Traditional) flagDutch flagFrench flagGerman flag
    Greek flagItalian flagJapanese flagKorean flagPortuguese flagRussian flag
    Spanish flag     
    By N2H
  • Also Find Me On

    Delicious  Digg  Facebook  Flickr  FriendFeed  Last.fm  LinkedIn  LiveJournal  Ma.gnolia  MyBlogLog  MySpace  StumbleUpon  Technorati  Twitter  YouTube  



  • Find job
  • Problem Mortgage
  • Online Advertising
  • Bankruptcy
  • Mortgage