【问题标题】:How do I do a strtr on UTF-8 in PHP?如何在 PHP 中对 UTF-8 执行 strtr?
【发布时间】:2010-11-30 01:50:53
【问题描述】:

我正在为 PHP 寻找兼容 UTF-8 的 strtr。

【问题讨论】:

    标签: php unicode utf-8 strtr


    【解决方案1】:
    函数 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 数组的功能。

    【讨论】:

    • 效果很好。接受的答案应移至此。
    【解决方案2】:
    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 的签名。
    猜你喜欢
    • 1970-01-01
    • 2011-12-17
    • 2012-06-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    相关资源
    最近更新 更多