The functions themselves are certainly handy, though you might want to stick them into a class. The function names are not really coherent, so you might want to make it a bit more portable.
I think you should clean things up at every point data goes in/out.
What you're doing now is assume that the only thing that'll be done to the data is insertion into a database.
What if you want to render it into a page? Or into a plain txt file? Or into a textarea?
You should wait with doing mysql specific mutations until you're sure you're going to stick in into the database.
try this:
(You might want to make your functions array-proof, so you can throw in one value as well as an array)
<?php
class safe_io{
function from_request( &$data ){
/* remove backslashes if some annoying
antique nosy ini setting has put them in...
I made only this one by reference, cause
you shouldn't want to do anything with
the input if they MAY contain abundant backslashes
*/
}
function to_form( $data ){
/*
Make sure you don't $&^% up the form
so use some htmlentities for ' and/or "
' for ' doesn't work in all browsers.
*/
return $data;
}
function to_textarea( $data ){
/*
Not much action needed. It'll all work, except for </textarea>.
Be creative on this one.
*/
return $data ;
}
function to_db( $data ){
/*
The regular. Just slap in some backslashes.
*/
return $data ;
}
function to_html( $data ){
/*
htmlspecialchars() or even htmlentities()
*/
return $data ;
}
}
// usage:
safe_io::from_request( $_POST );
echo print_r( safe_io::to_html( $_POST ), true );
$ar_assoc_insert = safe_io::to_db( $_POST );
// 1) clean out values that aren't legal
// 2) smash the array into an insert query
?>