How does FILTER_VALIDATE_INT in PHP handle leading and trailing white spaces in input data?
When using FILTER_VALIDATE_INT in PHP, leading and trailing white spaces in input data will cause the validation to fail, as it expects the input to be a valid integer without any additional characters. To solve this issue, we can trim the input data before applying the filter to remove any leading and trailing white spaces.
// Input data with leading and trailing white spaces
$input = " 123 ";
// Trim the input data to remove leading and trailing white spaces
$filtered_input = trim($input);
// Validate the filtered input as an integer
if (filter_var($filtered_input, FILTER_VALIDATE_INT) !== false) {
echo "Valid integer input: " . $filtered_input;
} else {
echo "Invalid integer input";
}