PHP need to trim all the $_POST variables

前端 未结 8 1896
广开言路
广开言路 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:48

    Simply

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

    But if $_POST members (even if 1 of them) is again an array itself, then use 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;
        }
    

提交回复
热议问题