我有一个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 => "%");
但是,一旦我用alphabet1中的H
(nr 15)替换为alphabet1中的ḥ
,它就不再起作用了。
我认为它与识别unicode有关,因为只要这些单词不包含任何特殊符号,顺序就是正确的;但所有包含特殊符号的单词都放在结果的开头。
我试着看看unicode规范化;但我真的只是一个业余爱好者,所以这很难。
这是问题还是存在其他问题,我该如何解决?
答案 0 :(得分:3)
我已经在我的代码块中留下了所有的测试回声,并且只是将它们注释掉,以防您想要查看整个过程中生成的内容。
我对你的代码采取了一些自由。我不喜欢调用该函数的函数,我将查找数组压缩为空格导致的字符串。这将起到与从1开始的索引数组相同的效果。将查找从数组转换为字符串意味着我可以使用mb_strpos()
而不是array_search()
。
修复代码的关键点在于循环,特别是使用[$i]
访问字母。您看,您不能将这些多字节字符视为单字节字符 - 您必须使用mb_substr()
来访问&#34;整个&#34;信。
设置$alphabet
和encoding
的值意味着,您不必写第二个&#34;帮助&#34;函数传递所有必要的数据。 uksort()
将通过其预期的两个论点,一切顺利进行。
最后一条建议是:mb_
函数价格昂贵,因此请尽快在代码中尝试return
并将mb_
函数更远离&#34;下标& #34;无论何时在逻辑上可能。
以下是我建议的代码:(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_
函数,我之前提到过这些函数很昂贵&#34;。以下代码段提供相同的输出。
代码:(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);
答案 1 :(得分:0)
charset
标记中的meta
必须为UTF-8
。这就是外界所说的; MySQL将其称为utf8mb4
。
在MySQL内部,用COLLATION utf8mb4_unicode_520_ci
声明要排序的列的排序规则。这样,MySQL可以为您完成工作:
SELECT ... ORDER BY col ...