PHP needs to trim all $ _POST variables

You need help in a difficult situation. I need to trim all $ _POST variables.

Is there a way to do this with one shot, i.e. using one function?

I know that trim ($ _ POST) will not do, I need to do some function, for example

function sanatize_post(){ foreach ($_POST as $key => $val) $_POST[$key] = trim($val); } 

But if you have any other suggestions or comments, please help me.

thanks

+11
source share
10 answers

use array_walk with custom function

 $clean_values = array(); array_walk($_POST, 'sanitize_post'); function sanitize_post($item, $key) { $clean_values[$key] = trim($item); //optional further cleaning ex) htmlentities } 
+9
source

Just

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

But if the members of $ _POST (even if 1 of them) are again the array itself, then use the recursive version:

  function array_map_deep( $value, $callback ) { if ( is_array( $value ) ) { foreach ( $value as $index => $item ) { $value[ $index ] = array_map_deep( $item, $callback ); } } elseif ( is_object( $value ) ) { $object_vars = get_object_vars( $value ); foreach ( $object_vars as $property_name => $property_value ) { $value->$property_name = array_map_deep( $property_value, $callback ); } } else { $value = call_user_func( $callback, $value ); } return $value; } 
+13
source

array_walk($_POST, 'trim') (note that this and the idea may be broken, because the input name = foo [bar] is translated into an array)

Edit: the above is incorrect. Try $_POST = array_map('trim', $_POST); .

+8
source

Here's a one-liner that will also work either on single values ​​or recursively on arrays:

 $_POST = filter_var($_POST, \FILTER_CALLBACK, ['options' => 'trim']); 
+5
source

You can also use this code that I wrote if you want to misinform a string OR array with one function:

 function sanitize ($value) { // sanitize array or string values if (is_array($value)) { array_walk_recursive($value, 'sanitize_value'); } else { sanitize_value($value); } return $value; } function sanitize_value (&$value) { $value = trim(htmlspecialchars($value)); } 

Just use it like this:

 $post_sanitized = sanitize($_POST); $apple_sanitized = sanitize('apple'); 
+2
source

Just use this:

 array_walk($_POST, create_function('&$val', '$val = trim($val);')); 

and your $ _POST is now clipped.

+1
source

I think it is better to use anonymous functions:

 array_walk($_POST, function(& $value){ $value = trim($value); }); 
+1
source
  $_POST = array_map("trim", $_POST); 

I think this can help you!

+1
source

Other answers did not work with associative arrays. These recursive functions will clip all values ​​inside the $ _POST array at all levels down.

 // Trim all values of associative array function trim_associative_array(&$input_array) { if (is_array($input_array)) { foreach ($input_array as $key => &$val) { if (is_array($val)) { trim_associative_array($val); } else { $input_array[$key] = trim($val); } } } } 
+1
source

wow it all decided for me

I put it in the header and all my forms are now clean

Great tnx for this

0
source

All Articles