en ce qui me concerne, j'ai beaucoup personnalisé ma version du tuto mvc proposé par grafikart, qui au final n'en garde à l'identique que le systeme de routage, et j'utilise le code suivant comme generateur de slug:
/* Remplace caractères accentués d'une chaine */
/* Générateur de Slug (Friendly Url) : convertit un titre en une URL conviviale.*/
public static function makeSlug($string, $maxlen=0){
self::getInstance();
//cyrylic transcription
$cyrylicFrom = array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?');
$cyrylicTo = array('A', 'B', 'W', 'G', 'D', 'Ie', 'Io', 'Z', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'Ch', 'C', 'Tch', 'Sh', 'Shtch', '', 'Y', '', 'E', 'Iu', 'Ia', 'a', 'b', 'w', 'g', 'd', 'ie', 'io', 'z', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ch', 'c', 'tch', 'sh', 'shtch', '', 'y', '', 'e', 'iu', 'ia');
$from = array('Á', 'À', 'Â', 'Ä', '?', '?', 'Ã', 'Å', '?', 'Æ', '?', '?', '?', '?', 'Ç', '?', '?', 'Ð', 'É', 'È', '?', 'Ê', 'Ë', '?', '?', '?', '?', '?', '?', '?', '?', 'á', 'à', 'â', 'ä', '?', '?', 'ã', 'å', '?', 'æ', '?', '?', '?', '?', 'ç', '?', '?', 'ð', 'é', 'è', '?', 'ê', 'ë', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'I', 'Í', 'Ì', '?', 'Î', 'Ï', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'Ñ', '?', 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', '?', 'Ø', '?', 'Œ', '?', '?', '?', 'í', 'ì', 'i', 'î', 'ï', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'ñ', '?', 'ó', 'ò', 'ô', 'ö', 'õ', '?', 'ø', '?', 'œ', '?', '?', '?', '?', 'Š', '?', '?', '?', 'Þ', 'Ú', 'Ù', 'Û', 'Ü', '?', '?', '?', '?', '?', '?', '?', 'Ý', '?', 'Ÿ', '?', '?', 'Ž', '?', '?', '?', '?', 'š', '?', 'ß', '?', '?', 'þ', 'ú', 'ù', 'û', 'ü', '?', '?', '?', '?', '?', '?', '?', 'ý', '?', 'ÿ', '?', '?', 'ž');
$to = array('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'G', 'G', 'G', 'G', 'G', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g', 'g', 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'IJ', 'J', 'K', 'L', 'L', 'N', 'N', 'N', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'CE', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'ij', 'j', 'k', 'l', 'l', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'R', 'R', 'S', 'S', 'S', 'S', 'T', 'T', 'T', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'W', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z', 'r', 'r', 's', 's', 's', 's', 'B', 't', 't', 'b', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'w', 'y', 'y', 'y', 'z', 'z', 'z');
$numbers=array('0','1','2','3','4','5','6','7','8','9','-');
$from = array_merge($from, $cyrylicFrom);
$to = array_merge($to, $cyrylicTo);
$newstring=strtolower(str_replace($from, $to, trim($string)));
$newstring=str_replace(' ', '-', $newstring);
$newStringTab=array();
if(function_exists('str_split'))
{
$stringTab=str_split($newstring);
}
else
{
$slen=strlen($newstring);
for($i=0; $i<$slen; $i++)
{
$stringTab$i]=$newstring{$i};
}
}
foreach($stringTab as $letter)
{
if(in_array($letter, range('a', 'z')) || in_array($letter, $numbers))
{
$newStringTab]=$letter;
}
}
if(count($newStringTab))
{
$newString=implode($newStringTab);
if($maxlen>0)
{
$newString=substr($newString, 0, $maxlen);
}
$i=0;
do{
$newString = str_replace('--', '-', $newString);
$pos=strpos($newString, '--');
$i++;
if($i>100)
{
die('remove duplicates \'--\' loop error');
}
}while($pos!==false);
}
else
{
$newString='';
}
return $newString;
}
je mets ici une version plus courte et optimisée du générateur de slug ci-dessus:
(elle corrige en réalité les insuffisances de la fonction proposée par Xtr3me)
public static function makeSlug($str,$maxlen=0) {
self::getInstance();
$str = str_replace('&', 'et', trim($str));
// On convertit la chaîne en UTF-8 si besoin est.
if($str !== mb_convert_encoding(mb_convert_encoding($str,'UTF-32','UTF-8'),'UTF-8','UTF-32')) {
$str = mb_convert_encoding($str,'UTF-8');
}
$str = htmlentities($str, ENT_NOQUOTES ,'UTF-8');
// Quelques entités à remplacer par les lettres correspondantes.
$str = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|orn|slash|th|tilde|caron|lig);`i','$1',$str);
$str = preg_replace(array('`^a-z0-9]`i','`-]+`'),'-',$str);
if($maxlen>0)
{
$str=substr($str, 0, $maxlen);
}
return strtolower(trim($str,'-'));
}