Skip to content

Instantly share code, notes, and snippets.

@jeremytarpley
Last active March 24, 2018 15:15
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 jeremytarpley/a0f4cbab8e73ce194516aaf91ee2a786 to your computer and use it in GitHub Desktop.
Save jeremytarpley/a0f4cbab8e73ce194516aaf91ee2a786 to your computer and use it in GitHub Desktop.
php to strip out anything that isn't a letter, number or minus
<?php
/*
Had a need to go a little further than PHP's sanitize filters would get me. Basically wanted a simple white list.
ex: filter_var ($variable, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH) - still leaves a bunch of caracters I don't want in my variable
the preg_replace below will remove anything that isn't a letter, number or minus
*/
$clean_var = preg_replace('/[^a-z0-9-]/', '', $variable);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment