Skip to content

Instantly share code, notes, and snippets.

@RoverWire
Created November 20, 2014 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoverWire/246251d5ce80b845032f to your computer and use it in GitHub Desktop.
Save RoverWire/246251d5ce80b845032f to your computer and use it in GitHub Desktop.
String to Slug Converter
/**
* URL Slug
*
* Converts an string into a formated url slug string
*
* @access public
* @param string str
* @return string
*/
if (! function_exists('url_slug'))
{
function url_slug($str)
{
$a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ';
$b = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
$str = utf8_decode($str);
$str = strtr($str, utf8_decode($a), $b);
$str = strtolower(trim($str));
$str = preg_replace("/[^a-z0-9-]/", "-", $str);
$str = preg_replace("/-+/", "-", $str);
if(substr($str, strlen($str) - 1, strlen($str)) === "-") {
$str = substr($str, 0, strlen($str) - 1);
}
return utf8_encode($str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment