Skip to content

Instantly share code, notes, and snippets.

@eoghanobrien
Created May 5, 2012 13:44
Show Gist options
  • Select an option

  • Save eoghanobrien/2602550 to your computer and use it in GitHub Desktop.

Select an option

Save eoghanobrien/2602550 to your computer and use it in GitHub Desktop.
Modifies a string to remove all non ASCII characters and spaces.
/**
* Modifies a string to remove all non ASCII characters and spaces.
* Note : Works with UTF-8
*
* @param string $string The text to slugify
* @return string The slugified text
*/
function toSlug ($string) {
$string = utf8_decode($string);
$string = html_entity_decode($string);
$a = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ';
$b = 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn';
$string = strtr($string, utf8_decode($a), $b);
$ponctu = array("?", ".", "!", ",");
$string = str_replace($ponctu, "", $string);
$string = trim($string);
$string = preg_replace('/([^a-z0-9]+)/i', '-', $string);
$string = strtolower($string);
$string = trim($string, '-');
return utf8_encode($string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment