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


Tags: , , , , , ,
Posted in PHP, Web Development | 1 Comment »

Refactor My Code - I Wish I’d Thought of It!

Written by Chris on August 15, 2008 – 9:03 pm -

refactormycode.com is a fledgling website setup as a project by French Canadian  Ruby Developer Marc-André Cournoyer and basically it's like a coding forum without the usual forum junk, style and obfuscation of content (forums for me are always a pain to use because of the tiers of information you have to go through).

It's a great looking website covering all the current major programming languages (at least when thinking of the web) and the idea behind it, though simple, seems to work really well.

Basically, you have some code that works, but you want to make it better, more efficient, or just tidier. So, you post your code sample and other people suggest changes. It's kind of like yahoo answers for developers or the comments foot of the PHP manual.

The code to be refactored, so far in the PHP section at least, has been of a reasonable standard i.e. that of at least intermediate developers, which is great as these services can tend to get flooded by newbies who don't know their $i++ from their ++$i :p and rapidly lose interest for me.

So far there are only a few PHP samples on there to comment on but I think, as the site begins to grow, there will be a wealth of well developed and critiqued code that serves as good examples or directly useable functionality.

So far I can't really fault it, other than making the "Best" link clearer by calling it "Best Refactorors"  or something similar, and providing some closed, or accepted answer(s) type functionality to stop a thread getting out of hand (it could be that this exists already but I just haven't seen it yet). Maybe even an option to download each refactoring as a plain text file could be useful.

So, to round up, as you can probably tell, I love this site and you can see my standing in the community in the foot of this page! Keep up the great work Marc and I hope your site develops in the way it deserves and gets the recognition it should!



Popularity: 96% [?]

Sphere: Related Content


Tags: , , , , , , , , ,
Posted in Flash, JavaScript, PHP, Web Development | 1 Comment »

Wordpress 2.5 Released!

Written by Chris on March 31, 2008 – 8:32 am -

Version 2.5 of wordpress has been released and we have a nice new admin interface at last, not to mention the plugin auto-updater (something I was planning to write myself sometime soon) and a number of other nice features!

Personally I think the update is a great, but I can't help wondering if, as other wordpress users upgrade, they will find nicer = better?

It may be just the fact that I was used to the old interface that causes me to say this, but somehow, though clunky, the old interface seemed easier to use. From a new user perspective however, the interface is probably more obvious to navigate and generally deal with, so when it comes to attracting new users to wordpress then it's probably a good thing - after all this isn't going to deter wordpress's current x million users, hopefully just attract more!

So what else is new?

Well there's a nice little gallery making the display of collections of images a LOT easier. Couple this with the multi-file up-loader and the EXIF extractor and we now have a system that really aids those who display a lot of multimedia content in their blogs.

At last (I wish I could emphasise this more), the search function covers the scope of a whole wordpress site i.e. posts and pages - about time!

Tag and permalink management has been vastly improved so there's no need to use a third party solution to control these.

And the last most significant upgrade is the improvement (some would say actual implementation) of sensible multi-user management during editing. i.e. If user x is authoring a post along with user y, user x now actually locks the post while editing, meaning that user y's updates and auto-saves can't overwrite or conflict with those of user x.

From a technical point of view, there are some good security updates well worth having, as well as features that allow you to override and add to the new media buttons feature of the editor. There is also the shortcode API which really eases the placement of special code within the content of a standard post without screwing up the html of either - again the insertion of media galleries is a prime example of this.

All round then, the new version shows some really useful progression in wordpress and can only aid its uptake by new users, as well as adding some good solid updates for those of us who have been using it for years!



Popularity: 13% [?]

Sphere: Related Content


Tags: , , , , ,
Posted in Wordpress | 1 Comment »

Meld Wordpress And Facebook Together With Wordbook

Written by Chris on January 30, 2008 – 6:18 pm -

Now this is a pretty neat plugin:

Wordbook allows you to send your wordpress blog posts directly to your Facebook mini feed without any cut and paste nastyness - fantatsic if you tend to used wordpress as a blog and Facebook for networking rather than the other way round :)

You can get the plugin here: http://wordpress.org/extend/plugins/wordbook/

Popularity: 13% [?]

Sphere: Related Content


Tags: , , , , , , , , ,
Posted in Wordpress | No Comments »
RSS