我有一个 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 => "%");
但是,一旦我将alphabet2中的H
(nr 15)替换为alphabet1中的ḥ
,它就不再起作用了。
我想这与识别unicode有关,因为只要单词不包含任何特殊符号,顺序就是正确的;但所有包含特殊符号的单词都会放在结果的开头。
我尝试查看 unicode 规范化;但我真的只是一个业余爱好者,所以这相当困难。
这是问题所在还是还有其他问题?我该如何解决?
最佳答案
我在代码块中保留了所有测试回显,只是将它们注释掉,以防您想查看整个过程中生成的内容。
我对你的代码做了一些改动。我不喜欢函数调用函数,因此我将查找数组压缩为以空格开头的字符串。这将与从 1 开始的索引数组具有相同的效果。将查找从数组转换为字符串意味着我可以使用 mb_strpos()
而不是 array_search()
.
代码中需要修复的关键点是循环,特别是使用 [$i]
访问字母。您会看到,您不能将这些多字节字符视为单字节字符 - 您必须使用 mb_substr()
来访问“整个”字母。
设置 $alphabet
和 encoding
的值意味着,您不必编写第二个“帮助器”函数来传递所有必要的数据。 uksort()
将传递其预期的两个参数,一切都会顺利进行。
最后一条建议是:mb_
函数的开销很大,因此请务必尽快在代码中return
并保留 mb_
只要逻辑上可能,code> 就会进一步“下写”。
这是我建议的代码:( 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_
函数,我之前提到这些函数“昂贵”。以下代码片段提供相同的输出。
代码:(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);
关于php - 如何使用预定义的字母表对 unicode 中的字符串进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49920018/