Created
June 10, 2012 23:28
-
-
Save MikeRogers0/2907692 to your computer and use it in GitHub Desktop.
Filter Functions in PHP
This file contains hidden or 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 protected]', FILTER_VALIDATE_EMAIL)); // Returns: string(17) "[email protected]" | |
// 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)[email protected]', FILTER_SANITIZE_EMAIL )); // Returns: string(17) "[email protected]" | |
// 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 protected]'; // 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