I have a question that will probably be easy for someone to answer.

I have a form with about 20 fields +- to be entered...

When submitted, I check for the validity and existance of a few of the fields that are important, but not all of them

What I would like to do is to sort of clean up every field using a function that I use on some other individual field input forms.

My question is, how would I itrate through all of the $POST data and send each through the function, without calling each one by name?

Here is the function, although I don't know that it is relavent to the question:

function cleanString($string){
    return $string;
}//end function

I've used that function for so long that I've forgotten what exactly it does... I think it removes unnecessary white space and protects against malicious data entry... at least that is what I hope it does.

Thanks in advance for your feedback


5 Years
Discussion Span
Last Post by showman13

For a simple cleaning measure like mysql_real_escape_string() for everything, you can use array_map():

$cleanpost = array_map('mysql_real_escape_string',$_POST);

However, this doesn't validate your data (obviously). So all your POST vars are now in the $cleanpost array, e.g. $_POST['surname'] will be cleaned to $cleanpost['surname'].

If you need something more 'custom' you can apply user functions:

$cleanpost = array_map('myCleaner',$_POST);
function myCleaner($string){
    return htmlentities(mysql_real_escape_string($string));

Edited by diafol


Thank You ardav... that sounds like exactly what I was looking for...

Edited by showman13

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.