Last active
April 15, 2020 20:27
-
-
Save jasperfrontend/e6d60a512fdf2b6cea50ea568680e85d to your computer and use it in GitHub Desktop.
sanitize user input but it's overdone
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 | |
// clean up job for domain search input | |
function sanitize_user_input( $string ) { | |
$string = str_replace('-', ' ', $string); // save the hyphen! | |
$sclean = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string); // murder the rest | |
$hyphen = str_replace(' ', '-', $sclean); // bring back the hyphen! | |
$ltrim = ltrim($hyphen, '-'); // hyphen can never be first | |
$return = rtrim($ltrim, '-'); // hyphen can never be last | |
return strtolower($return); | |
} | |
// input: abc%^&*d-ef.net | |
// expected (hoped for) output: abcd-ef.net | |
// current output: acd-efnet | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment