php 如何使用strtr()来翻译多字节/重音/变音字符?

q0qdq0h2  于 2023-02-07  发布在  PHP
关注(0)|答案(4)|浏览(118)

有人有strtr()函数的多字节变体吗?

所需用法示例:

Example:
$from = 'ľľščťžýáíŕďňäô'; // these chars are in UTF-8
$to   = 'llsctzyairdnao';

// input - in UTF-8
$str  = 'Kŕdeľ ďatľov učí koňa žrať kôru.';
$str  = mb_strtr( $str, $from, $to );

// output - str without diacritic
// $str = 'Krdel datlov uci kona zrat koru.';
0md85ypi

0md85ypi1#

我 * 相信 * strtr is multi-byte safe,无论哪种方式,因为str_replace * 是 * 多字节安全的,您可以 Package 它:

function mb_strtr($str, $from, $to)
{
  return str_replace(mb_str_split($from), mb_str_split($to), $str);
}

由于没有mb_str_split函数,您还需要编写自己的函数(使用mb_substrmb_strlen),或者您可以只使用PHP UTF-8实现(略有更改):

function mb_str_split($str) {
    return preg_split('~~u', $str, null, PREG_SPLIT_NO_EMPTY);;

}

然而,如果你想找一个函数来删除字符串中的所有(拉丁?)重音,你可能会发现下面的函数很有用:

function Unaccent($string)
{
    return preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'));
}

echo Unaccent('ľľščťžýáíŕďňä'); // llsctzyairdna
echo Unaccent('Iñtërnâtiônàlizætiøn'); // Internationalizaetion
pbgvytdp

pbgvytdp2#

function mb_strtr($str,$map,$enc){
$out="";
$strLn=mb_strlen($str,$enc);
$maxKeyLn=1;
foreach($map as $key=>$val){
    $keyLn=mb_strlen($key,$enc);
    if($keyLn>$maxKeyLn){
        $maxKeyLn=$keyLn;
    }
}
for($offset=0; $offset<$strLn; ){
    for($ln=$maxKeyLn; $ln>=1; $ln--){
        $cmp=mb_substr($str,$offset,$ln,$enc);
        if(isset($map[$cmp])){
            $out.=$map[$cmp];
            $offset+=$ln;
            continue 2;
        }
    }
    $out.=mb_substr($str,$offset,1,$enc);
    $offset++;
}
return $out;
}
tjvv9vkg

tjvv9vkg3#

使用str_replace可能是一个不错的解决方案。

<?php
header('Content-Type: text/plain;charset=utf-8');

function my_strtr($inputStr, $from, $to, $encoding = 'UTF-8') {
        $inputStrLength = mb_strlen($inputStr, $encoding);

        $translated = '';

        for($i = 0; $i < $inputStrLength; $i++) {
                $currentChar = mb_substr($inputStr, $i, 1, $encoding);

                $translatedCharPos = mb_strpos($from, $currentChar, 0, $encoding);

                if($translatedCharPos === false) {
                        $translated .= $currentChar;
                }
                else {
                        $translated .= mb_substr($to, $translatedCharPos, 1, $encoding);
                }
        }

        return $translated;
}

$from = 'ľľščťžýáíŕďňä'; // these chars are in UTF-8
$to   = 'llsctzyairdna';

// input - in UTF-8
$str  = 'Kŕdeľ ďatľov učí koňa žrať kôru.';

print 'Original: ';
print chr(10);
print $str;

print chr(10);
print chr(10);

print 'Tranlated: ';
print chr(10);
print my_strtr( $str, $from, $to);

使用PHP 5.2在我的机器上打印:

Original: 
Kŕdeľ ďatľov učí koňa žrať kôru.

Tranlated: 
Krdel datlov uci kona zrat kôru.
plicqrtu

plicqrtu4#

strtr() has two valid signatures for receiving its parameters.
您实现strtr()的方式执行逐字节转换--这显然不适合多字节字符。

$from = 'ľľščťžýáíŕďňäô'; // these chars are in UTF-8
$to   = 'llsctzyairdnao';

$str  = 'Kŕdeľ ďatľov učí koňa žrať kôru.';
echo strtr($str, $from, $to);
// Kd�deyn y�atynov uyaa� kod�a dnradr ka�ru.

正确的实现是向函数提供一个要翻译的字符的关联数组--这是多字节安全的方法。

$trans = [
    'ľ' => 'l',
    'š' => 's',
    'č' => 'c',
    'ť' => 't',
    'ž' => 'z',
    'ý' => 'y',
    'á' => 'a',
    'í' => 'i',
    'ŕ' => 'r',
    'ď' => 'd',
    'ň' => 'n',
    'ä' => 'a',
    'ô' => 'o',
];
echo strtr($str, $trans);
// Krdel datlov uci kona zrat koru.

还应当注意,存在被开发来处理这样的任务的库和本机函数。

相关问题