PHP filters are used for validating and sanitizing data. They become particularly useful when you want to secure input data in PHP scripts. PHP has many built-in functions to handle common input validation and sanitization tasks.
Here's how you can sanitize an email address.
<?php
$email = "hello@example.com";
$sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL);
echo $sanitized_email; // Outputs: hello@example.com
?>
In this example, the <h1> tags are stripped from the email address.
You can validate an email address like so:
<?php
$email = "hello@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "This email address is considered valid.";
} else {
echo "This email address is considered invalid.";
}
?>
In this example, `filter_var` returns the email if it is valid, otherwise it returns `false`.
Both validation and sanitization filters can take optional flags and options to alter their behavior.
<?php
$options = array(
"options" => array(
"min_range" => 0,
"max_range" => 100
)
);
$age = 110;
if (!filter_var($age, FILTER_VALIDATE_INT, $options)) {
echo("Variable value is not within the legal range");
} else {
echo("Variable value is within the legal range");
}
?>
Here, we specify a range for an integer. The value must be between 0 and 100. If it's outside this range, `filter_var` will return `false`.
<?php
$ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
echo "This is a valid IPv6 address.";
} else {
echo "This is not a valid IPv6 address.";
}
?>
In this example, the `FILTER_FLAG_IPV6` flag specifies that the function should validate an IPv6 address.
You can also directly filter input data without having to manually fetch it. For example, if you have a query string `?age=30`, you can filter it directly like this:
<?php
$age = filter_input(INPUT_GET, 'age', FILTER_VALIDATE_INT);
?>
This function is particularly useful for filtering GET or POST data without having to put it into variables first.
You can also define your own filter functions using callbacks.
<?php
function validate_odd($value) {
if ($value % 2 == 1) {
return true;
} else {
return false;
}
}
$options = array(
"options" => array("validate_odd")
);
$input = 3;
if (filter_var($input, FILTER_CALLBACK, $options)) {
echo "3 is odd";
} else {
echo "Number is not odd";
}
?>
In this example, `filter_var` uses a custom function `validate_odd` to determine if the input is an odd number or not.
These are some of the advanced aspects of PHP filters. You can combine them in various ways to validate and sanitize data according to your specific needs.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here