PHP need to trim all the $_POST variables

前端 未结 8 1902
广开言路
广开言路 2021-01-01 23:14

Need you help in an unusal situation. I need to trim all the $_POST variables.

Is there any way I can do it at a single shot i.e., using a single function?

I

8条回答
  •  清歌不尽
    2021-01-01 23:56

    The other answers did not work well with associative arrays. This recursive functions will trim all values inside a $_POST array 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);
                }
            }
        }
    }
    

提交回复
热议问题