Filter and Sanitize

filter_input

// Sanitize input
if (is_numeric($_POST[$post_key])) {
    $filtered_db_inputs[$post_key] = filter_input(INPUT_POST, $post_key, FILTER_SANITIZE_NUMBER_INT);
} else {
    $filtered_db_inputs[$post_key] = filter_input(INPUT_POST, $post_key, FILTER_SANITIZE_STRING);
}

Trimming

Use trim, ltrim, and rtrim to remove substrings from the beginning and end of a string.

$string = ' _hello_ ';
$trimmed = trim( $string ); // Returns "_hello_"
echo trim( $trimmed, '_' ); // Returns "hello"

// Warning 'ltrim' and 'rtrim' remove all instances of all the characters passed
// to the function. It is not a string definition.
echo ltrim( $string, ' _' ); // Returns "hello_ "
echo = rtrim( $string, '_ ' ); // Returns " _hello"