【发布时间】: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