Skip to content

Instantly share code, notes, and snippets.

@jasperfrontend
Last active April 15, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasperfrontend/e6d60a512fdf2b6cea50ea568680e85d to your computer and use it in GitHub Desktop.
Save jasperfrontend/e6d60a512fdf2b6cea50ea568680e85d to your computer and use it in GitHub Desktop.
sanitize user input but it's overdone
<?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