How do I do a strtr on UTF-8 in PHP?

joeforker picture joeforker · Sep 21, 2009 · Viewed 9k times · Source

I'm looking for a UTF-8 compatible strtr for PHP.

Answer

joeforker picture joeforker · Sep 21, 2009
function strtr_utf8($str, $from, $to) {
    $keys = array();
    $values = array();
    preg_match_all('/./u', $from, $keys);
    preg_match_all('/./u', $to, $values);
    $mapping = array_combine($keys[0], $values[0]);
    return strtr($str, $mapping);
}