Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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