PHP strtr不起作用

I'm searching for some safe URL generator for a long time, but can't found one that works well with Hungarian language, so I'm decided to replace the hungarian characters with the strtr function, but it's not working properly. It doesn't replace anything just the " " to "-".

function safeURL($str) {
    $clean = strtr($str, array('Á'=>'a', 'É'=>'e', 'Í'=>'i', 'Ú'=>'u', 'Ü'=>'u', 'Ű'=>'u', 'Ó'=>'o', 'Ö'=>'o', 'Ő'=>'o', '.'=>'-', '!'=>'-', '.'=>'?'));
    $clean = strtr($clean, array('á'=>'a', 'é'=>'e', 'í'=>'i', 'ú'=>'u', 'ü'=>'u', 'ű'=>'u', 'ó'=>'o', 'ö'=>'o', 'ő'=>'o', ' '=>'-', '/'=>'-', ':'=>'-'));
    $clean = trim($clean, '-');
    return $clean;
}

It looks a bit odd that I need to list all of the capital and non-capital letters, but strtolower doesn't seem to work with these characters too. What am I doing wrong? (The encoding on the page and in the database was properly set to UTF-8).

Outputs:

Üdvözöllek a weboldalamon!  =>  Üdvözöllek-a-weboldalamon
Sziasztok üpegvőreúű        =>  Sziasztok-üpegvőreúű

Why not to use special extension for that. Derick's Transliterate is pretty good. Take a look - maybe you can use it. http://derickrethans.nl/projects.html look in the middle of the page for "Transliteration PHP Extension" there is also an example under 'Read more'

Try that :

$string = "IÁm DÍYÁR";

//These are the chars that are going to be replaced.. you can add other chars by yourself
$bad_chars = array('Á', 'É', 'Í', 'Ú', 'Ü', 'Ű', 'Ó'); 

//the bad chars are going to be replaced by those ones by order
$good_chars = array('A', 'E', 'I', 'U', 'U', 'U', 'O');


$safe_str = strtr($string, array_combine($bad_chars, $good_chars));

or in your situation here is a function :

function safeURL($str) {
    $capital_bad_chars = array('Á', 'É', 'Í', 'Ú', 'Ü', 'Ű', 'Ó', 'Ö', 'Ő', '.', '!', '.');
    $capital_good_chars = array('a', 'e', 'i', 'u', 'u', 'u', 'o', 'o', 'o', '-', '-', '?');

    $small_bad_chars = array('á', 'é', 'í', 'ú', 'ü', 'ű', 'ó', 'ö', 'ő', ' ', '/', ':'));
    $small_good_chars = array('a', 'e', 'i', 'u', 'u', 'u', 'o', 'o', 'o', '-', '-', '-');

    $clean = strtr($str, array_combine($capital_bad_chars, $capital_good_chars));
    $clean = strtr($str, array_combine($small_bad_chars, $small_good_chars));

    $clean = trim($clean, '-');
    return $clean;
}

that worked for me !

You need to use multibyte string functions for correct processing of UTF-8 chars. Try to use str_replace and mb_strtolower function. According PHP documentation str_replace is binary safe function. This means that it can handle UTF-8 encoded string without any data loss.