9911dc64 |
1 | <?php |
4b4abf93 |
2 | |
9911dc64 |
3 | /** |
4 | * cp1251 encoding functions |
5 | * |
6 | * takes a string of unicode entities and converts it to a cp1251 encoded string |
7 | * Unsupported characters are replaced with ?. |
8 | * |
47ccfad4 |
9 | * @copyright © 2004-2006 The SquirrelMail Project Team |
4b4abf93 |
10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
9911dc64 |
11 | * @version $Id$ |
9911dc64 |
12 | * @package squirrelmail |
13 | * @subpackage encode |
14 | */ |
15 | |
16 | /** |
17 | * Converts string to cp1251 |
18 | * @param string $string text with numeric unicode entities |
19 | * @return string cp1251 encoded text |
20 | */ |
21 | function charset_encode_cp1251 ($string) { |
22 | // don't run encoding function, if there is no encoded characters |
23 | if (! preg_match("'&#[0-9]+;'",$string) ) return $string; |
24 | |
25 | $string=preg_replace("/&#([0-9]+);/e","unicodetocp1251('\\1')",$string); |
26 | // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetocp1251(hexdec('\\1'))",$string); |
91e0dccc |
27 | |
9911dc64 |
28 | return $string; |
29 | } |
30 | |
31 | /** |
32 | * Return cp1251 symbol when unicode character number is provided |
91e0dccc |
33 | * |
34 | * This function is used internally by charset_encode_cp1251 |
598294a7 |
35 | * function. It might be unavailable to other SquirrelMail functions. |
91e0dccc |
36 | * Don't use it or make sure, that functions/encode/cp1251.php is |
37 | * included. |
9911dc64 |
38 | * |
39 | * @param int $var decimal unicode value |
40 | * @return string cp1251 character |
41 | */ |
42 | function unicodetocp1251($var) { |
43 | |
44 | $cp1251chars=array('160' => "\xA0", |
45 | '164' => "\xA4", |
46 | '166' => "\xA6", |
47 | '167' => "\xA7", |
48 | '169' => "\xA9", |
49 | '171' => "\xAB", |
50 | '172' => "\xAC", |
51 | '173' => "\xAD", |
52 | '174' => "\xAE", |
53 | '176' => "\xB0", |
54 | '177' => "\xB1", |
55 | '181' => "\xB5", |
56 | '182' => "\xB6", |
57 | '183' => "\xB7", |
58 | '187' => "\xBB", |
59 | '1025' => "\xA8", |
60 | '1026' => "\x80", |
61 | '1027' => "\x81", |
62 | '1028' => "\xAA", |
63 | '1029' => "\xBD", |
64 | '1030' => "\xB2", |
65 | '1031' => "\xAF", |
66 | '1032' => "\xA3", |
67 | '1033' => "\x8A", |
68 | '1034' => "\x8C", |
69 | '1035' => "\x8E", |
70 | '1036' => "\x8D", |
71 | '1038' => "\xA1", |
72 | '1039' => "\x8F", |
73 | '1040' => "\xC0", |
74 | '1041' => "\xC1", |
75 | '1042' => "\xC2", |
76 | '1043' => "\xC3", |
77 | '1044' => "\xC4", |
78 | '1045' => "\xC5", |
79 | '1046' => "\xC6", |
80 | '1047' => "\xC7", |
81 | '1048' => "\xC8", |
82 | '1049' => "\xC9", |
83 | '1050' => "\xCA", |
84 | '1051' => "\xCB", |
85 | '1052' => "\xCC", |
86 | '1053' => "\xCD", |
87 | '1054' => "\xCE", |
88 | '1055' => "\xCF", |
89 | '1056' => "\xD0", |
90 | '1057' => "\xD1", |
91 | '1058' => "\xD2", |
92 | '1059' => "\xD3", |
93 | '1060' => "\xD4", |
94 | '1061' => "\xD5", |
95 | '1062' => "\xD6", |
96 | '1063' => "\xD7", |
97 | '1064' => "\xD8", |
98 | '1065' => "\xD9", |
99 | '1066' => "\xDA", |
100 | '1067' => "\xDB", |
101 | '1068' => "\xDC", |
102 | '1069' => "\xDD", |
103 | '1070' => "\xDE", |
104 | '1071' => "\xDF", |
105 | '1072' => "\xE0", |
106 | '1073' => "\xE1", |
107 | '1074' => "\xE2", |
108 | '1075' => "\xE3", |
109 | '1076' => "\xE4", |
110 | '1077' => "\xE5", |
111 | '1078' => "\xE6", |
112 | '1079' => "\xE7", |
113 | '1080' => "\xE8", |
114 | '1081' => "\xE9", |
115 | '1082' => "\xEA", |
116 | '1083' => "\xEB", |
117 | '1084' => "\xEC", |
118 | '1085' => "\xED", |
119 | '1086' => "\xEE", |
120 | '1087' => "\xEF", |
121 | '1088' => "\xF0", |
122 | '1089' => "\xF1", |
123 | '1090' => "\xF2", |
124 | '1091' => "\xF3", |
125 | '1092' => "\xF4", |
126 | '1093' => "\xF5", |
127 | '1094' => "\xF6", |
128 | '1095' => "\xF7", |
129 | '1096' => "\xF8", |
130 | '1097' => "\xF9", |
131 | '1098' => "\xFA", |
132 | '1099' => "\xFB", |
133 | '1100' => "\xFC", |
134 | '1101' => "\xFD", |
135 | '1102' => "\xFE", |
136 | '1103' => "\xFF", |
137 | '1105' => "\xB8", |
138 | '1106' => "\x90", |
139 | '1107' => "\x83", |
140 | '1108' => "\xBA", |
141 | '1109' => "\xBE", |
142 | '1110' => "\xB3", |
143 | '1111' => "\xBF", |
144 | '1112' => "\xBC", |
145 | '1113' => "\x9A", |
146 | '1114' => "\x9C", |
147 | '1115' => "\x9E", |
148 | '1116' => "\x9D", |
149 | '1118' => "\xA2", |
150 | '1119' => "\x9F", |
151 | '1168' => "\xA5", |
152 | '1169' => "\xB4", |
153 | '8211' => "\x96", |
154 | '8212' => "\x97", |
155 | '8216' => "\x91", |
156 | '8217' => "\x92", |
157 | '8218' => "\x82", |
158 | '8220' => "\x93", |
159 | '8221' => "\x94", |
160 | '8222' => "\x84", |
161 | '8224' => "\x86", |
162 | '8225' => "\x87", |
163 | '8226' => "\x95", |
164 | '8230' => "\x85", |
165 | '8240' => "\x89", |
166 | '8249' => "\x8B", |
167 | '8250' => "\x9B", |
168 | '8364' => "\x88", |
169 | '8470' => "\xB9", |
170 | '8482' => "\x99"); |
171 | |
172 | if (array_key_exists($var,$cp1251chars)) { |
173 | $ret=$cp1251chars[$var]; |
174 | } else { |
175 | $ret='?'; |
176 | } |
177 | return $ret; |
178 | } |