3

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.

0plus1
  • 4,475
  • 12
  • 47
  • 89
  • possible duplicate of [PHP Transliteration](http://stackoverflow.com/questions/1284535/php-transliteration) and [Is there any free Opensource PHP Translit Lib](http://stackoverflow.com/questions/2482812/is-there-any-free-opensource-php-translit-lib/2482831#2482831) – Gordon Jul 12 '10 at 16:20
  • 2
    Only a tip. If you are replacing single characters you could use `strtr`, it's actually faster than `str_replace`: `strtr($string, 'òóàèéù', 'ooaeeu')`. But this obviously doesn't solve the encoding problem. Could you please tell what encoding you saved the file in? – NikiC Jul 12 '10 at 16:26

3 Answers3

7

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
Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

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.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
1

as simple as

preg_replace('/[^-\w]/', '', preg_replace('/\s+/', '-', iconv('UTF-8', 'ASCII//TRANSLIT', $s)))
softwarevamp
  • 827
  • 10
  • 14