Skip to content

Instantly share code, notes, and snippets.

@sebgates
Created August 21, 2017 07:14
Show Gist options
  • Save sebgates/3224acc2f797b3c0b113ba8095e028fb to your computer and use it in GitHub Desktop.
Save sebgates/3224acc2f797b3c0b113ba8095e028fb to your computer and use it in GitHub Desktop.
<?php
function slug($text){
// replace non letter or digits by -
$text = preg_replace('~[^pLd]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment