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 :]
-
1It is worth noting that you really should set up a new array of the trimmed values, rather than editing global variables. – rybo111 Feb 28 '14 at 12:31
you can do this with array_map
:
$_POST = array_map('trim', $_POST);
-
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 Aug 26 '09 at 19:11
-
5I do not recommend using this, as it shows an error if your $_POST contains an array (e.g. checkboxes). See valdas.mistolis's answer below. – rybo111 Nov 4 '13 at 14:50
Works with multi-dimensional arrays
array_walk_recursive($_POST, function (&$val)
{
$val = trim($val);
});
-
3Since many forms contain checkboxes, this should be the accepted answer. – rybo111 Nov 4 '13 at 14:41
foreach($_POST as &$p) $p = trim($p);
-
This will also have same problem pointed out by @rybo111 in a comment on another answer above.
$p
is not always a string. It can also be an array. – rineez Jan 5 '16 at 3:56 -
@rineez, that's right. I did upvote the
array_walk_recursive
answer ;-) – Michael Krelin - hacker Jan 5 '16 at 8:11
Quick and simple:
foreach($_POST as $key => $val)
{
$_POST[$key] = trim($val);
}
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
Update based on a comment below
Sometimes the $_POST array may contain arrays. If you want to trim contents of those arrays as well, there are many custom implementations of array_map_recursive
available in the PHP manual user notes. Go there and choose one for yourself. If you don't like to take a custom implementation, array_walk_recursive
is also a good option for you.
-
@inakiabt: No, but there is a custom implementation of array_map_recursive in the PHP manual user notes. – Alix Axel Aug 26 '09 at 17:49
You can do this with array_walk()
.
-
The callback function for
array_walk
takes two parameters, one for the value and one for the key. But the second parameter oftrim
is intended for a list of character that should be removed from the begin and end. So it wouldn’t work. But witharray_map
it would. – Gumbo Aug 26 '09 at 17:22 -
It you wrote a simple wrapper for trim, array_walk would work in place. function aw_trim(&$str){ if(is_string($str)){ $str = trim($str); } } array_walk($_POST,'aw_trim'); – txyoji Aug 27 '09 at 0:31
Using recursive
function you can do that.
PHP
// Static $_POST Array.
$_POST['1']='one ';
$_POST['2']=' two';
$_POST['3'][]=' three ';
$_POST['4'][][]=' four';
$_POST['5'][0][1][3]='five ';
// Recursive function for trim data.
function trim_recursive($array){
$return = array();
foreach($array as $key=>$values){
if(is_array($values)===true){
$return[$key] = trim_recursive($values);
}
else{
$return[$key] = trim($values);
}
}
return $return;
}
// Usage.
$_POST = trim_recursive($_POST);
Output
// Output before trim.
array(5) {
[1]=>
string(4) "one "
[2]=>
string(4) " two"
[3]=>
array(1) {
[0]=>
string(9) " three "
}
[4]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(5) " four"
}
}
[5]=>
array(1) {
[0]=>
array(1) {
[1]=>
array(1) {
[3]=>
string(5) "five "
}
}
}
}
// Output after trim.
array(5) {
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
array(1) {
[0]=>
string(5) "three"
}
[4]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(4) "four"
}
}
[5]=>
array(1) {
[0]=>
array(1) {
[1]=>
array(1) {
[3]=>
string(4) "five"
}
}
}
}