我正在为 PHP 寻找兼容 UTF-8 的 strtr。
8977 次
2 回答
17
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);
}
于 2009-09-21T13:03:02.317 回答
2
函数 strtr_utf8($str, $from, $to)
{
$keys = 数组();
$values = 数组();
if(!is_array($from))
{
preg_match_all('/./u', $from, $keys);
preg_match_all('/./u', $to, $values);
$mapping = array_combine($keys[0], $values[0]);
}别的
$映射=$来自;
返回 strtr($str, $mapping);
}
我稍微编辑了 joeforker 的函数以返回使用第二个参数作为 replace_pairs 数组的功能。
于 2013-12-22T04:32:20.377 回答