Skip to content

Instantly share code, notes, and snippets.

@PoisonousJohn
Created August 9, 2012 13:48
Show Gist options
  • Save PoisonousJohn/3304319 to your computer and use it in GitHub Desktop.
Save PoisonousJohn/3304319 to your computer and use it in GitHub Desktop.
/**
* source : http://snipplr.com/view/22741/slugify-a-string-in-php/
*
* @static
* @param $text
* @return mixed|string
*/
static public function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
$chars = array(
'ґ'=>'g','ё'=>'e','є'=>'e','ї'=>'i','і'=>'i',
'а'=>'a', 'б'=>'b', 'в'=>'v',
'г'=>'g', 'д'=>'d', 'е'=>'e', 'ё'=>'e',
'ж'=>'zh', 'з'=>'z', 'и'=>'i', 'й'=>'i',
'к'=>'k', 'л'=>'l', 'м'=>'m', 'н'=>'n',
'о'=>'o', 'п'=>'p', 'р'=>'r', 'с'=>'s',
'т'=>'t', 'у'=>'u', 'ф'=>'f', 'х'=>'h',
'ц'=>'c', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'sch',
'ы'=>'y', 'э'=>'e', 'ю'=>'u', 'я'=>'ya', 'é'=>'e', '&'=>'and',
'ь'=>'', 'ъ' => '',
);
$text = mb_strtolower($text, 'utf-8');
$text = strtr($text, $chars);
// transliterate
if (function_exists('iconv'))
{
$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