How to trim all $_POST in form

I assume the method will be:


I dont know if thats right or atleast it will trim all inputs

trim doesn’t take an array so you’d need to pass each value in the array to it separately,

reset ($_POST);
while (list ($key, $val) = each ($_POST)) {$val = trim($val);}

not quite understand.

can i write this ?


$email = trim($_POST[‘email’]);


$_POST[‘email’] = trim($_POST[‘email’]);

But that trims just 1 $_POST field, of course. Using a loop like felgall showed you, you can trim them all without having to code them one by one.

or can i write this ?


$e= mysql_real_escape_string($_POST['mail']);


$_POST = array_map('trim', $_POST);


$email= mysql_real_escape_string(trim($_POST[‘mail’]));

You should have other validation on an email field prior to trying to insert it into a database that should be run prior to passing it through mysql_real_escape_string.

Also, running everything through mysql_real_escape_string automatically doesn’t help when you switch database types… I normally do that as the last thing, usually ‘inline’ in the query, or use a database abstraction layer

Be careful if you have any fields passed through as an array.

Example… 5 checkboxes all named categories

trim on $_POST[‘categories’] will break the array.