PHP need to trim all the $_POST variables

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

    You can also use this code I wrote in case you want to sanitize a string OR an 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));
    }
    

    Simply use it like this:

    $post_sanitized = sanitize($_POST);
    $apple_sanitized = sanitize('apple');
    

提交回复
热议问题