Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Created March 3, 2014 16:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dhrrgn/9328159 to your computer and use it in GitHub Desktop.
Save dhrrgn/9328159 to your computer and use it in GitHub Desktop.
Slugify. Note: Requires PHP >= 5.4 and the Intl extension
<?php
function slugify($string, $toLower = true) {
$rules = "Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;";
if ($toLower) {
$rules .= ' Lower();';
}
$string = transliterator_transliterate($rules, $string);
// Remove repeating hyphens and spaces (e.g. 'foo---bar' becomes 'foo-bar')
$string = preg_replace('/[-\s]+/', '-', $string);
return trim($string, '-');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment