-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPinyin.class.php
74 lines (65 loc) · 1.52 KB
/
Pinyin.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
class Pinyin {
var $map_1st;
var $map_2nd;
var $code;
var $py;
function __construct() {
$tmp = file_get_contents('./pinyin-1st.json');
$this->map_1st = json_decode($tmp, true);
$this->code = array_values($this->map_1st);
$this->py = array_keys($this->map_1st);
$tmp = file_get_contents('./pinyin-2nd.json');
$this->map_2nd = json_decode($tmp, true);
}
public function get($str) {
$str = $this->utf8_to_gbk($str);
$res = $this->pins($str);
return strtolower($res);
}
private function utf8_to_gbk($str){
return iconv('UTF-8', 'GB2312//IGNORE', $str);
}
//二分查找,第一级汉字
private function search($code, $min, $max){
if($max - $min == 1){
if($code > $this->code[$min]){
return $this->py[$max];
} else {
return $this->py[$min];
}
} else {
$mid = intval(($min + $max) / 2);
if($code > $this->code[$mid]){
return $this->search($code, $mid, $max);
} else {
return $this->search($code, $min, $mid);
}
}
}
private function code_to_py($code){
if($code < 5590){
return $this->search($code, 0, count($this->code) - 1);
}
$mapEx = $this->map_2nd;
foreach ($mapEx as $k => $v) {
if(in_array($code, $v)){
return $k;
}
}
return '';
}
private function pins($str){
$res = '';
for($i = 0; $i < strlen($str); $i++){
$code = ord(substr($str, $i, 1));
if($code > 160) {
$code = $code - 160;
$foo = ord(substr($str, ++$i, 1)) - 160;
$code = $code * 100 + $foo;
}
$res .= $this->code_to_py($code);
}
return $res;
}
}