For some reason (it's a long story) I need to change the accents with their 'normal' counterparts.
I'm doing this:
$array = array(
'ò' => 'o',
'ó' => 'o',
'à' => 'a',
'è' => 'e',
'é' => 'e',
'ù' => 'u',
);
return str_replace(array_keys($array), array_values($array), $string);
but it doesn't work (with normal letter works like a charm) I think it's an encoding problem, is there another way to do this? How can I fix this?
Thank you.
From URL Friendly Username in PHP? and slightly modified
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'))), ' '));
}
$user = 'Alix Axel';
echo Slug($user); // alix axel
$user = 'Álix Ãxel';
echo Slug($user); // alix axel
$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix axel
strtr is used to translate certain characters. Here is an example from the comments:
// php.net/strtr comment by: allixsenos at gmail dot com
function normalize ($string) {
$table = array(
'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
);
return strtr($string, $table);
}
unfortunately you do still need to list any character translations. This, from the comments, seems to be pretty complete.
as simple as
preg_replace('/[^-\w]/', '', preg_replace('/\s+/', '-', iconv('UTF-8', 'ASCII//TRANSLIT', $s)))