Could you trim all $_POST vars? because i have a very long list right now for trim each var. looks very unprofessional. i thought trim($_POST); would maybe work but it didnt :]
Nice and elegant.
ceejayoz
2009-08-26 17:18:05
+2
A:
Quick and simple:
foreach($_POST as $key => $val)
{
$_POST[$key] = trim($val);
}
zombat
2009-08-26 17:15:54
The callback function for `array_walk` takes two parameters, one for the value and one for the key. But the second parameter of `trim` is intended for a list of character that should be removed from the begin and end. So it wouldn’t work. But with `array_map` it would.
Gumbo
2009-08-26 17:22:28
It you wrote a simple wrapper for trim, array_walk would work in place. function aw_trim( } } array_walk($_POST,'aw_trim');
txyoji
2009-08-27 00:31:04
Right, like it even more than my own (and vote for it), wish it could do it in-place without assigning the array.
Michael Krelin - hacker
2009-08-26 19:11:26
A:
The simplest, and cleanest (in my opinion), is to use the built in array_map function:
array_map('trim', $_POST);
You can also apply a method of your own by passing an array as the first callback-parameter like so:
array_map(array('My_Class', 'staticMethod'), $_POST); // Invoke a static method
array_map(array($myObject, 'objectMethod'), $_POST);
// Invoke $myObject->objectMethod for each element of $_POST
PatrikAkerstrand
2009-08-26 17:27:05
+1
A:
By careful that $_POST can contain an array. array_map("trim",$elm) will break the array.
mich
2010-02-27 00:24:34