Filter Functions in 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 | |
// Filter an Email Address | |
var_dump(filter_var('email@example.com', FILTER_VALIDATE_EMAIL)); // Returns: string(17) "email@example.com" | |
// This is a fake email being filtered. | |
var_dump(filter_var('fake_mail.com', FILTER_VALIDATE_EMAIL)); // Returns: bool(false) | |
var_dump(filter_var('ema(i)l@example.com', FILTER_SANITIZE_EMAIL )); // Returns: string(17) "email@example.com" | |
// Filter a URL | |
var_dump(filter_var('example.com', FILTER_VALIDATE_URL)); // Returns: bool(false) | |
// Filter a URL | |
var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL)); // Returns: string(18) "http://example.com" | |
// Example usage | |
$email = 'email@example.com'; // or something submitted from a form. | |
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ // If this returns false | |
die('The email you send is invalid.'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment