views:

46

answers:

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.

+2  A: 

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
John Conde
Worked like a charm thank you very much.
0plus1
A: 

Not sure if foreach is an option to you but if so this works fine to replace it.

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);

$test = 'whát dó í hávê to change.';

$replace = array(
      'a' => array('á','à','â','ä'),
      'e' => array('è','ê','ë','é'),
      'i' => array('í','ì','î','ï'),
      'o' => array('ó','ò','ô','ö'),
      'u' => array('ù','û','ü')
);

foreach ($replace as $with_this=>$change_this) {
      $test = str_replace($change_this,$with_this,$test);
}
echo $test;

?>
Prix
+1  A: 

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