Created
July 23, 2024 05:19
-
-
Save alexlatam/2e802c0457aeb21213b8ecefbb960291 to your computer and use it in GitHub Desktop.
Filter and sanitize data with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// VALIDATIONS | |
filter_var($integerNumber, FILTER_VALIDATE_INT); // verify if variable is an integer number | |
filter_var("127.0.0.1", FILTER_VALIDATE_IP); // verify if is an IP code | |
filter_var("test@mail.com", FILTER_VALIDATE_EMAIL); // verify if is a correct email | |
filter_var("https://domain.ext/", FILTER_VALIDATE_URL); // verify if is a correct url | |
filter_var("true", FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); // verify if is a boolean value | |
filter_var("123.45", FILTER_VALIDATE_FLOAT); // verify if is a float number | |
//SANITIZE DATA | |
$string = filter_var("<h1>Hi there</h1>", FILTER_SANITIZE_STRING); // sanitize an string. removed html code form string | |
$sanitized_string = filter_var("<h1>Hello, World!</h1>", FILTER_SANITIZE_FULL_SPECIAL_CHARS); // out: <h1>Hello, World!</h1> | |
$sanitized_email = filter_var("test@@domain.com", FILTER_SANITIZE_EMAIL); // out: test@domain.com | |
$sanitized_url = filter_var("https://www.domain.ext/<script>", FILTER_SANITIZE_URL); // out: https://www.domain.ext/ | |
$sanitized_int = filter_var("123abc", FILTER_SANITIZE_NUMBER_INT); // out: 123 | |
// SOME UTILS VALIDATIONS | |
// IPV4 Validation | |
$ip = "192.168.1.1"; | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | |
echo "The IP address (IPv4) is valid."; | |
} else { | |
echo "The IP address (IPv4) is not valid."; | |
} | |
// IPV6 Validation | |
$ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; | |
$options = array( | |
'flags' => FILTER_FLAG_IPV6 | |
); | |
if (filter_var($ip, FILTER_VALIDATE_IP, $options)) { | |
echo "The IP address (IPv6) is valid."; | |
} else { | |
echo "The IP address (IPv6) is not valid."; | |
} | |
// Validate a number range | |
$options = array( | |
'options' => array( | |
'min_range' => 1, | |
'max_range' => 100, | |
) | |
); | |
$int = 50; | |
if (filter_var($int, FILTER_VALIDATE_INT, $options)) { | |
echo "The number is inside the range."; | |
} else { | |
echo "The number is not inside the range."; | |
} | |
// Validate a negative integer number | |
$int = 25; | |
if (filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range" => 0)))) { | |
echo "The number is a negative integer number."; | |
} else { | |
echo "The number is not a negative integer number."; | |
} | |
// Validate a correct domain | |
$domain = "example.com"; | |
if (filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { | |
echo "El dominio es válido."; | |
} else { | |
echo "El dominio no es válido."; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment