Skip to content

Instantly share code, notes, and snippets.

@ankr
Last active August 29, 2015 14:18
Show Gist options
  • Save ankr/c82eb71ba61bff43612d to your computer and use it in GitHub Desktop.
Save ankr/c82eb71ba61bff43612d to your computer and use it in GitHub Desktop.
PHP function to turn a string into a slug. Will transliterate characters and replace whitespace with `-`.
<?php
// @see http://stackoverflow.com/a/13331948/1640765
function slugify($str) {
$str = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $str);
$str = preg_replace('/\s+/', '-', $str);
return trim($str, '-');
}
echo slugify(" Radical'épil - Antibes"); // radicalepil-antibes
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment