如何在PHP中的UTF-8上进行strtr?
我正在为PHP寻找一个UTF-8兼容的strtr如何在PHP中的UTF-8上进行strtr?,php,unicode,utf-8,strtr,Php,Unicode,Utf 8,Strtr,我正在为PHP寻找一个UTF-8兼容的strtr 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]); r
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);
}
函数strtr_utf8($str,$from,$to)
{
$keys=array();
$values=array();
如果(!is_数组($from))
{
preg_match_all('//u',$from,$keys);
preg_match_all('//u',$to$值);
$mapping=array_combine($key[0],$value[0]);
}否则
$mapping=$from;
返回strtr($str,$mapping);
}
我稍微编辑了JoeFook的函数,以返回使用第二个参数作为Repest-Alxon数组的功能。
你应该考虑第二个参数也可以是一个映射的数组。我不需要这个,但是它会更忠实于STRR的签名。它工作得很好。被接受的答案应该移到这个问题上。 function strtr_utf8($str, $from, $to) { $keys = array(); $values = array(); if(!is_array($from)) { preg_match_all('/./u', $from, $keys); preg_match_all('/./u', $to, $values); $mapping = array_combine($keys[0], $values[0]); }else $mapping=$from; return strtr($str, $mapping); }