【问题标题】:How to sorting strings in unicode using a predefined alphabet?如何使用预定义的字母对 unicode 中的字符串进行排序?
【发布时间】:2018-04-19 11:31:27
【问题描述】:

我有一个 mysql 表,其中包含 unicode 中的单词,使用 š 等符号。 表中的列定义为utf8mb4_general_ci,并识别上述符号。

在我放的网页标题中

<meta http-equiv="Content-Type" content="text/html; charset=utf8mb4">

此网页包含一个将数据发送到 php 页面的表单。在php页面的开头我放了:

mysqli_set_charset($con,"utf8mb4");

在此页面中,我进行了 mysql 搜索,我得到了一个数组,这个数组 ($result) 必须使用我生成的字符查找数组按其键排序,其中包括单字节和多字节字符。

这是数组:

Array ( 
[nṯr] => Array ( [0] => Ka.C.Coptite.urkVIII,176b [1] => Ka.C.Coptite.urkVIII,177,1 ) 
[n] => Array ( [0] => Ka.C.Coptite.urkVIII,176c [1] => Ka.C.Coptite.urkVIII,177,1 [2] => Ka.C.Coptite.urkVIII,177,2 ) 
[nḫȝḫȝ] => Array ( [0] => Ka.C.Coptite.urkVIII,176c ) 
[nwj] => Array ( [0] => Ka.C.Coptite.urkVIII,176c ) 
[nfr] => Array ( [0] => Ka.C.Coptite.urkVIII,176c [1] => Ka.C.Coptite.urkVIII,177,2 ) 
[nḥḥ] => Array ( [0] => Ka.C.Coptite.urkVIII,176e [1] => Ka.C.Coptite.urkVIII,177,1 [2] => Ka.C.Coptite.urkVIII,177,1 ) 
[nḏ] => Array ( [0] => Ka.C.Coptite.urkVIII,177,1 ) 
)

我做的是:

uksort($result, 'compare_keys_by_alphabet');

这里指的是函数:

function compare_keys_by_alphabet($a, $b)
{
    static $alphabet = array( 1 => "-" , 2 => "," , 3 => ".", 4 => "ȝ", 5 => "j", 6 => "ʿ", 7 => "w", 8 => "b", 9 => "p", 10 => "f", 11 => "m", 12 => "n", 13 => "r", 14 => "h", 15 => "ḥ", 16 => "ḫ", 17 => "ẖ", 18 => "s", 19 => "š", 20 => "q", 21 => "k", 22 => "g", 23 => "t", 24 => "ṯ", 25 => "d", 26 => "ḏ", 27 => "⸗", 28 => "/", 29 => "(", 30 => ")", 31 => "[", 32 => "]", 33 => "<", 34 => ">", 35 => "{", 36 => "}", 37 => "'", 38 => "*", 39 => "#", 40 => "I", 41 => "0", 42 => "1", 43 => "2", 44 => "3", 45 => "4", 46 => "5", 47 => "6", 48 => "7", 49 => "8", 50 => "9", 51 => "&", 52 => "@", 53 => "%");

    return compare_by_alphabet($alphabet, $a, $b);
}

使用:

function compare_by_alphabet(array $alphabet, $str1, $str2) {
    $c = max(strlen($str1), strlen($str2));

    for ($i = 0; $i < $c; $i++) {
        $s1 = $str1[$i];
        $s2 = $str2[$i];
        //if ($s1===$s2) continue;
        $i1 = array_search($s1, $alphabet);
        //if ($i1===false) continue;
        $i2 = array_search($s2, $alphabet);
        //sif ($i2===false) continue;
        if ($i2==$i1) continue;
        if ($i1 < $i2) return -1;
        else return 1;
    }
    return 0;
}

这与非 unicode 字母表完美搭配:

static $alphabet2 = array( 1 => '-' , 2 => ',' , 3 => '.' , 4 => "A", 5 => "j", 6 => "a", 7 => "w", 8 => "b", 9 => "p", 10 => "f", 11 => "m", 12 => "n", 13 => "r", 14 => "h", 15 => "H", 16 => "x", 17 => "X", 18 => "s", 19 => "S", 20 => "q", 21 => "k", 22 => "g", 23 => "t", 24 => "T", 25 => "d", 26 => "D", 27 => "=", 28 => "/", 29 => "(", 30 => ")", 31 => "[", 32 => "]", 33 => "<", 34 => ">", 35 => "{", 36 => "}", 37 => "'", 38 => "*", 39 => "#", 40 => "I", 41 => "1", 42 => "2", 43 => "3", 44 => "4", 45 => "5", 46 => "6", 47 => "7", 48 => "8", 49 => "9", 50 => "0", 51 => "&", 52 => "@", 53 => "%");

但是一旦我用字母表 2 中的 H (nr 15) 替换了字母表 1 中的 ,它就不再起作用了。

我想这与识别unicode有关,因为只要单词不包含任何特殊符号,顺序是正确的;但所有包含特殊符号的单词都放在结果的开头。

我尝试查看 unicode 规范化;但我真的只是个业余爱好者,所以这很难。

这是问题还是有其他问题,我该如何解决?

【问题讨论】:

    标签: php sorting unicode multibyte-functions multibyte-characters


    【解决方案1】:

    我将所有测试回显都留在了我的代码块中,只是将它们注释掉,以防您想查看整个过程中生成的内容。

    我对你的代码有些随意。我不喜欢调用该函数的函数,我将您的查找数组压缩为一个以空格为主导的字符串。这将起到与从 1 开始的索引数组相同的效果。将查找从数组转换为字符串意味着我可以使用 mb_strpos() 而不是 array_search()

    在代码中修复的关键点在于循环,特别是使用[$i] 访问字母。你看,你不能把这些多字节字符当作单字节字符——你必须使用mb_substr()来访问“整个”字母。

    $alphabetencoding 设置值意味着,您不必编写第二个“帮助”函数来传递所有必要的数据。 uksort() 将传递其预期的两个参数,一切顺利进行。

    最后一条建议是:mb_ 函数很昂贵,因此请务必尽快在代码中使用return,并在逻辑可能的情况下将mb_ 函数留得更远。

    这是我建议的代码:(Demo)

    function alphabetize_custom($a, $b, $alphabet = " -,.ȝjʿwbpfmnrhḥḫẖsšqkgtṯdḏ⸗/()[]<>{}'*#I0123456789&@%", $encoding = 'UTF-8') {
        //echo "\n----\n$a =vs= $b";
        $mb_length = max(mb_strlen($a, $encoding), mb_strlen($b, $encoding));
        for ($i = 0; $i < $mb_length; ++$i) {
            //echo "\n";
            $a_char = mb_substr($a, $i, 1, $encoding);
            $b_char = mb_substr($b, $i, 1, $encoding);
            //echo "$a_char -vs- $b_char\n";
            //echo "(" , mb_strlen($a_char, $encoding), " & ", mb_strlen($b_char, $encoding), ")\n";
            if ($a_char === $b_char) {/*echo "identical, continue";*/ continue;}
            if (!mb_strlen($a_char, $encoding)) { /* echo "a is empty -1";*/ return -1;}
            if (!mb_strlen($b_char, $encoding)) { /*echo "b is empty 1";*/ return 1;}
            $a_offset = mb_strpos($alphabet, $a_char, 0, $encoding);
            $b_offset = mb_strpos($alphabet, $b_char, 0, $encoding);
            //echo "[" , $a_offset, " & ", $b_offset, "]\n";
            if ($a_offset == $b_offset) { /*echo "== offsets, continue";*/ continue;}
            if ($a_offset < $b_offset) { /*echo "a offset -1";*/ return -1;}
            //echo "b offset 1";
            return 1;
        }
        //echo "0";
        return 0;
    }
    
    $result = [
        "nṯr" => ["Ka.C.Coptite.urkVIII,176b", "Ka.C.Coptite.urkVIII,177,1"],
        "n" => ["Ka.C.Coptite.urkVIII,176c", "Ka.C.Coptite.urkVIII,177,1", "Ka.C.Coptite.urkVIII,177,2"],
        "nḫȝḫȝ" => ["Ka.C.Coptite.urkVIII,176c"],
        "nwj" => ["Ka.C.Coptite.urkVIII,176c"],
        "nfr" => ["Ka.C.Coptite.urkVIII,176c", "Ka.C.Coptite.urkVIII,177,2"],
        "nḥḥ" => ["Ka.C.Coptite.urkVIII,176e", "Ka.C.Coptite.urkVIII,177,1", "Ka.C.Coptite.urkVIII,177,1"],
        "nḏ" => ["Ka.C.Coptite.urkVIII,177,1"]
    ];
    
    uksort($result, 'alphabetize_custom');
    
    var_export($result);
    

    输出:

    array (
      'n' => 
      array (
        0 => 'Ka.C.Coptite.urkVIII,176c',
        1 => 'Ka.C.Coptite.urkVIII,177,1',
        2 => 'Ka.C.Coptite.urkVIII,177,2',
      ),
      'nwj' => 
      array (
        0 => 'Ka.C.Coptite.urkVIII,176c',
      ),
      'nfr' => 
      array (
        0 => 'Ka.C.Coptite.urkVIII,176c',
        1 => 'Ka.C.Coptite.urkVIII,177,2',
      ),
      'nḥḥ' => 
      array (
        0 => 'Ka.C.Coptite.urkVIII,176e',
        1 => 'Ka.C.Coptite.urkVIII,177,1',
        2 => 'Ka.C.Coptite.urkVIII,177,1',
      ),
      'nḫȝḫȝ' => 
      array (
        0 => 'Ka.C.Coptite.urkVIII,176c',
      ),
      'nṯr' => 
      array (
        0 => 'Ka.C.Coptite.urkVIII,176b',
        1 => 'Ka.C.Coptite.urkVIII,177,1',
      ),
      'nḏ' => 
      array (
        0 => 'Ka.C.Coptite.urkVIII,177,1',
      ),
    )
    

    为了比较起见,我编写了一个替代代码块,它使用 array_search() 就像您的原始代码一样,并且毫不奇怪,根据 3v4l.org 上的速度测试,它似乎更有效。这可能是由于删除了几个我之前提到的“昂贵”的 4 个mb_ 函数。以下 sn-p 提供相同的输出。

    代码:(Demo)

    function alphabetize_custom($a, $b) {
        $alphabet = [' ', '-', ',', '.', 'ȝ', 'j', 'ʿ', 'w', 'b', 'p', 'f', 'm', 'n', 'r', 'h', 'ḥ', 'ḫ', 'ẖ', 's', 'š', 'q', 'k', 'g', 't', 'ṯ', 'd', 'ḏ', '⸗', '/', '(', ')', '[', ']', '<', '>', '{', '}', "'", '*', '#', 'I', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '&', '@', '%'];
        unset($alphabet[0]);  // removes dummy first key, effectively starting the keys from 1
        $encoding = 'UTF-8';
    
        $mb_length = max(mb_strlen($a, $encoding), mb_strlen($b, $encoding));
        for ($i = 0; $i < $mb_length; ++$i) {
            $a_char = mb_substr($a, $i, 1, $encoding);
            $b_char = mb_substr($b, $i, 1, $encoding);
            if ($a_char === $b_char) continue;
    
            $a_key = array_search($a_char, $alphabet);
            $b_key = array_search($b_char, $alphabet);
            if ($a_key === $b_key) continue;
    
            return $a_key - $b_key;
        }
        return 0;
    }
    
    $result = [
        "nṯr" => ["Ka.C.Coptite.urkVIII,176b", "Ka.C.Coptite.urkVIII,177,1"],
        "n" => ["Ka.C.Coptite.urkVIII,176c", "Ka.C.Coptite.urkVIII,177,1", "Ka.C.Coptite.urkVIII,177,2"],
        "nḫȝḫȝ" => ["Ka.C.Coptite.urkVIII,176c"],
        "nwj" => ["Ka.C.Coptite.urkVIII,176c"],
        "nfr" => ["Ka.C.Coptite.urkVIII,176c", "Ka.C.Coptite.urkVIII,177,2"],
        "nḥḥ" => ["Ka.C.Coptite.urkVIII,176e", "Ka.C.Coptite.urkVIII,177,1", "Ka.C.Coptite.urkVIII,177,1"],
        "nḏ" => ["Ka.C.Coptite.urkVIII,177,1"]
    ];
    
    uksort($result, 'alphabetize_custom');
    
    var_export($result);
    

    【讨论】:

    • 这非常有效。感谢您向我介绍 mb_ 功能。
    • @Preys 我正在考虑我发布的代码块,并意识到我可以制作一个更好的版本。我建议使用它而不是我的第一次尝试。如果您不介意 $alphabet 语法很长,您可以省略 unset() 调用,只需像原来一样声明数组。
    【解决方案2】:

    meta 标签中的charset 必须是UTF-8。这就是外界所说的; MySQL 称之为utf8mb4

    在 MySQL 中,使用COLLATION utf8mb4_unicode_520_ci 声明要排序的列的排序规则。有了它,MySQL 可以为您完成工作:

    SELECT ... ORDER BY col ...
    

    【讨论】:

      猜你喜欢
      • 2010-11-09
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多