Skip to content

Instantly share code, notes, and snippets.

@tjhole
Created April 9, 2014 11:41
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 tjhole/10258841 to your computer and use it in GitHub Desktop.
Save tjhole/10258841 to your computer and use it in GitHub Desktop.
PHP: String to ID
function toID($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment