Skip to content

Instantly share code, notes, and snippets.

@Mezzle
Created February 13, 2013 14:31
Show Gist options
  • Save Mezzle/4944982 to your computer and use it in GitHub Desktop.
Save Mezzle/4944982 to your computer and use it in GitHub Desktop.
UTF-8 Safe slugifier (PHP)
<?php
function slugify($text)
{
// Swap out Non "Letters" with a -
$text = preg_replace('/[^\\pL\d]+/u', '-', $text);
// Trim out extra -'s
$text = trim($text, '-');
// Convert letters that we have left to the closest ASCII representation
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// Make text lowercase
$text = strtolower($text);
// Strip out anything we haven't been able to convert
$text = preg_replace('/[^-\w]+/', '', $text);
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment