I have this trimmer function, it recursively trims all values in array (people put tons of spaces for no reason!):
function trimmer(&$var) {
if (is_array($var)) {
foreach($var as &$v) {
trimmer($v);
}
}
else {
$var = trim($var);
}
}
trimer($_POST);
PROBLEM: I would like to add new feature: i want this function also to convert all _ (underscore) in keys to spaces. I know how to convert keys (str_replace('_', ' ', $key)), but i have trouble to make it work in this recursive style…
Input:
$_POST['Neat_key'] = ' dirty value ';
Expected result:
$_POST['Neat key'] = 'dirty value';
Unfortunately, there isn’t a way to replace the keys of an array while you loop the array. This is a part of the language, the only way around it is to use a temporary array:
Try it: http://codepad.org/A0N5AU2g