Skip to content

Instantly share code, notes, and snippets.

@ryun
Created June 29, 2012 16:51
Show Gist options
  • Save ryun/3019151 to your computer and use it in GitHub Desktop.
Save ryun/3019151 to your computer and use it in GitHub Desktop.
Slugify helper function
function to_slug($str, $replace=array(), $delimiter='_', $maxLength=200)
{
if(!empty($replace))
{
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("%[^-/+|\w ]%", '', $clean);
$clean = strtolower(trim(substr($clean, 0, $maxLength), $delimiter));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment