Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mikem33
Created February 28, 2019 17:57
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 mikem33/70c93923b750f50fd1ba6c4f8bccfe45 to your computer and use it in GitHub Desktop.
Save mikem33/70c93923b750f50fd1ba6c4f8bccfe45 to your computer and use it in GitHub Desktop.
Slugify PHP Function
function slugify($str) {
// Convert to lowercase and remove whitespace
$str = strtolower(trim($str));
// Replace high ascii characters
$chars = array("ä", "ö", "ü", "ß");
$replacements = array("ae", "oe", "ue", "ss");
$str = str_replace($chars, $replacements, $str);
$pattern = array("/(é|è|ë|ê)/", "/(ó|ò|ö|ô)/", "/(ú|ù|ü|û)/");
$replacements = array("e", "o", "u");
$str = preg_replace($pattern, $replacements, $str);
// Remove puncuation
$pattern = array(":", "!", "?", ".", "/", "'");
$str = str_replace($pattern, "", $str);
// Hyphenate any non alphanumeric characters
$pattern = array("/[^a-z0-9-]/", "/-+/");
$str = preg_replace($pattern, "-", $str);
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment