1
2
3/**
4
5*中速版,中等内存使用,使用于一般需求或有大量重复字的大段文本
6
7*@text:待转换的字符串
8
9*@table_file:转换映射表文件名
10
11*/
12
13function encode_trans1 ( $text , $table_file = 'gb2big5' ) {
14
15$fp = fopen ( $table_file . '.table' , "r" );
16
17$cache = array();
18
19$max = strlen ( $text )- 1 ;
20
21for( $i = 0 ; $i < $max ; $i ++) {
22
23$h = ord ( $text [ $i ]);
24
25if( $h >= 160 ) {
26
27$l = ord ( $text [ $i \+ 1 ]);
28
29if( $h == 161 && $l == 64 ) {
30
31$text [ $i ]= " " ;
32
33} else{
34
35$cut = substr ( $text , $i , 2 );
36
37if(! $cache [ $cut ]) {
38
39fseek ( $fp ,( $h \- 160 )* 510 +( $l \- 1 )* 2 );
40
41$cache [ $cut ] = fread ( $fp , 2 );
42
43}
44
45$text [ $i ] = $cache [ $cut ][ 0 ];
46
47$text [++ $i ] = $cache [ $cut ][ 1 ];
48
49}
50
51}
52
53}
54
55fclose ( $fp );
56
57return $text ;
58
59}
60
61
62
63/**
64
65*低速版,最低内存使用,使用于少量字符时
66
67*@text:待转换的字符串
68
69*@table_file:转换映射表文件名
70
71*/
72
73function encode_trans2 ( $text , $table_file = 'gb2big5' ) {
74
75$fp = fopen ( $table_file . '.table' , "r" );
76
77$max = strlen ( $text )- 1 ;
78
79for( $i = 0 ; $i < $max ; $i ++) {
80
81$h = ord ( $text [ $i ]);
82
83if( $h >= 160 ) {
84
85$l = ord ( $text [ $i \+ 1 ]);
86
87if( $h == 161 && $l == 64 ) {
88
89$gb = " " ;
90
91}else{
92
93fseek ( $fp ,( $h \- 160 )* 510 +( $l \- 1 )* 2 );
94
95$gb = fread ( $fp , 2 );
96
97}
98
99$text [ $i ]= $gb [ 0 ];
100
101$text [ $i \+ 1 ]= $gb [ 1 ]; $i ++;
102
103}
104
105}
106
107fclose ( $fp );
108
109return $text ;
110
111}
112
113/**
114
115*高速版,最高内存使用,使用于大段文本时
116
117*@text:待转换的字符串
118
119*@table_file:转换映射表文件名
120
121*/
122
123function encode_trans3 ( $text , $table_file = 'gb2big5' ) {
124
125$fp = fopen ( $table_file . '.table' , "r" );
126
127$str = fread ( $fp , strlen ( $table_file . '.table' ));
128
129fclose ( $fp );
130
131$max = strlen ( $text )- 1 ;
132
133for( $i = 0 ; $i < $max ; $i ++) {
134
135$h = ord ( $text [ $i ]);
136
137if( $h >= 160 ) {
138
139$l = ord ( $text [ $i \+ 1 ]);
140
141if( $h == 161 && $l == 64 ) {
142
143$text [ $i ]= ' ' ;
144
145$text [++ $i ]= ' ' ;
146
147}else{
148
149$pos = ( $h \- 160 )* 510 +( $l \- 1 )* 2 ;
150
151$text [ $i ]= $str [ $pos ];
152
153$text [++ $i ]= $str [ $pos \+ 1 ];
154
155}
156
157}
158
159}
160
161return $text ;
162
163}
164
看到有人贴简繁转换的程序
comments powered by Disqus