Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. filter_var() / filter_input()

filter_var() / filter_input()

Since: PHP 5.2(2006)

Functions for validating and sanitizing input values. Use them to ensure the safety of data received from external sources.

Syntax

// Applies a filter to a variable.
filter_var($value, $filter, $options);

// Retrieves a value from $_GET, $_POST, $_COOKIE, etc. and applies a filter.
filter_input($type, $var_name, $filter, $options);

Function List

FunctionDescription
filter_var($value, $filter, $options)Applies a filter to a variable for validation or sanitization. Returns false if validation fails.
filter_input($type, $var_name, $filter, $options)Retrieves external input with a filter applied. Pass INPUT_GET, INPUT_POST, INPUT_COOKIE, etc. as the first argument.

Common Validation Filters

FilterDescription
FILTER_VALIDATE_EMAILValidates the format of an email address.
FILTER_VALIDATE_URLValidates the format of a URL.
FILTER_VALIDATE_IPValidates the format of an IP address. Use FILTER_FLAG_IPV4 or FILTER_FLAG_IPV6 to restrict the version.
FILTER_VALIDATE_INTValidates an integer value. You can specify a minimum and maximum range via options.
FILTER_VALIDATE_FLOATValidates a floating-point number.
FILTER_VALIDATE_BOOLEANValidates a boolean value. Recognizes true, yes, on, and 1 as true. ※ Note that passing the string 'false' returns null, not true — this is counterintuitive behavior, so be careful.
FILTER_VALIDATE_DOMAINValidates the format of a domain name. Available in PHP 7.0 and later.

Common Sanitization Filters

FilterDescription
FILTER_SANITIZE_EMAILRemoves characters that are invalid in an email address.
FILTER_SANITIZE_URLRemoves characters that are invalid in a URL.
FILTER_SANITIZE_NUMBER_INTRemoves all characters except digits, plus signs, and minus signs.
FILTER_SANITIZE_NUMBER_FLOATRemoves all characters except digits, plus signs, minus signs, and periods.
FILTER_SANITIZE_SPECIAL_CHARSEncodes HTML special characters.
FILTER_SANITIZE_ADD_SLASHESPerforms the same processing as addslashes(). Added in PHP 8.1 as a replacement for FILTER_SANITIZE_STRING.

Sample Code

sample_filter_var.php
<?php
// Validate an email address.
$email = "user@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) {
	echo "Valid email address."; // Passes validation.
}

// An invalid email address returns false.
var_dump(filter_var("not-an-email", FILTER_VALIDATE_EMAIL)); // Outputs: bool(false)

// Validate a URL.
$url = "https://wp-p.info";
if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
	echo "Valid URL.";
}

// Validate an integer with a range constraint.
$age = "25";
$options = ['options' => ['min_range' => 0, 'max_range' => 150]];
$valid_age = filter_var($age, FILTER_VALIDATE_INT, $options);
if ($valid_age !== false) {
	echo "Age: " . $valid_age; // Outputs: Age: 25
}

// Validate an IP address.
var_dump(filter_var("192.168.1.1", FILTER_VALIDATE_IP)); // Outputs: string(11) "192.168.1.1"
var_dump(filter_var("999.999.999.999", FILTER_VALIDATE_IP)); // Outputs: bool(false)

// Safely retrieve a $_GET value using filter_input().
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, [
	'options' => ['min_range' => 1, 'default' => 1]
]);

// Sanitization example.
$dirty_email = "user
このエントリーをはてなブックマークに追加