<?php
# Usage:
#
# $xc = new xconvert_cyr_string;
# $string = $xc->convert($string, $from, $to);
#
# $from and $to may accept the following values:
# 'k' -- koi8-r
# 'w' -- windows-1251
# 'i' -- iso8859-5
# 'a' -- x-cp866
# 'd' -- x-cp866
# 'm' -- x-mac-cyrillic
# 'u' -- UTF-8
class xconvert_cyr_string {
var $map, $rmap;
function xconvert_cyr_string() {
$this->map = array(
"d"=>array(128=>1040,129=>1041,130=>1042,131=>1043,132=>1044,
133=>1045,134=>1046,135=>1047,136=>1048,137=>1049,138=>1050,
139=>1051,140=>1052,141=>1053,142=>1054,143=>1055,144=>1056,
145=>1057,146=>1058,147=>1059,148=>1060,149=>1061,150=>1062,
151=>1063,152=>1064,153=>1065,154=>1066,155=>1067,156=>1068,
157=>1069,158=>1070,159=>1071,160=>1072,161=>1073,162=>1074,
163=>1075,164=>1076,165=>1077,166=>1078,167=>1079,168=>1080,
169=>1081,170=>1082,171=>1083,172=>1084,173=>1085,174=>1086,
175=>1087,176=>9617,177=>9618,178=>9619,179=>9474,180=>9508,
181=>9569,182=>9570,183=>9558,184=>9557,185=>9571,186=>9553,
187=>9559,188=>9565,189=>9564,190=>9563,191=>9488,192=>9492,
193=>9524,194=>9516,195=>9500,196=>9472,197=>9532,198=>9566,
199=>9567,200=>9562,201=>9556,202=>9577,203=>9574,204=>9568,
205=>9552,206=>9580,207=>9575,208=>9576,209=>9572,210=>9573,
211=>9561,212=>9560,213=>9554,214=>9555,215=>9579,216=>9578,
217=>9496,218=>9484,219=>9608,220=>9604,221=>9612,222=>9616,
223=>9600,224=>1088,225=>1089,226=>1090,227=>1091,228=>1092,
229=>1093,230=>1094,231=>1095,232=>1096,233=>1097,234=>1098,
235=>1099,236=>1100,237=>1101,238=>1102,239=>1103,240=>1025,
241=>1105,242=>1028,243=>1108,244=>1031,245=>1111,246=>1038,
247=>1118,248=>176,249=>8729,250=>183,251=>8730,252=>8470,
253=>164,254=>9632,255=>160),
"i"=>array(128=>128,129=>129,130=>130,131=>131,132=>132,133=>133,
134=>134,135=>135,136=>136,137=>137,138=>138,139=>139,140=>140,
141=>141,142=>142,143=>143,144=>144,145=>145,146=>146,147=>147,
148=>148,149=>149,150=>150,151=>151,152=>152,153=>153,154=>154,
155=>155,156=>156,157=>157,158=>158,159=>159,160=>160,161=>1025,
162=>1026,163=>1027,164=>1028,165=>1029,166=>1030,167=>1031,
168=>1032,169=>1033,170=>1034,171=>1035,172=>1036,173=>173,
174=>1038,175=>1039,176=>1040,177=>1041,178=>1042,179=>1043,
180=>1044,181=>1045,182=>1046,183=>1047,184=>1048,185=>1049,
186=>1050,187=>1051,188=>1052,189=>1053,190=>1054,191=>1055,
192=>1056,193=>1057,194=>1058,195=>1059,196=>1060,197=>1061,
198=>1062,199=>1063,200=>1064,201=>1065,202=>1066,203=>1067,
204=>1068,205=>1069,206=>1070,207=>1071,208=>1072,209=>1073,
210=>1074,211=>1075,212=>1076,213=>1077,214=>1078,215=>1079,
216=>1080,217=>1081,218=>1082,219=>1083,220=>1084,221=>1085,
222=>1086,223=>1087,224=>1088,225=>1089,226=>1090,227=>1091,
228=>1092,229=>1093,230=>1094,231=>1095,232=>1096,233=>1097,
234=>1098,235=>1099,236=>1100,237=>1101,238=>1102,239=>1103,
240=>8470,241=>1105,242=>1106,243=>1107,244=>1108,245=>1109,
246=>1110,247=>1111,248=>1112,249=>1113,250=>1114,251=>1115,
252=>1116,253=>167,254=>1118,255=>1119),
"k"=>array(128=>9472,129=>9474,130=>9484,131=>9488,132=>9492,
133=>9496,134=>9500,135=>9508,136=>9516,137=>9524,138=>9532,
139=>9600,140=>9604,141=>9608,142=>9612,143=>9616,144=>9617,
145=>9618,146=>9619,147=>8992,148=>9632,149=>8729,150=>8730,
151=>8776,152=>8804,153=>8805,154=>160,155=>8993,156=>176,
157=>178,158=>183,159=>247,160=>9552,161=>9553,162=>9554,
163=>1105,164=>9555,165=>9556,166=>9557,167=>9558,168=>9559,
169=>9560,170=>9561,171=>9562,172=>9563,173=>9564,174=>9565,
175=>9566,176=>9567,177=>9568,178=>9569,179=>1025,180=>9570,
181=>9571,182=>9572,183=>9573,184=>9574,185=>9575,186=>9576,
187=>9577,188=>9578,189=>9579,190=>9580,191=>169,192=>1102,
193=>1072,194=>1073,195=>1094,196=>1076,197=>1077,198=>1092,
199=>1075,200=>1093,201=>1080,202=>1081,203=>1082,204=>1083,
205=>1084,206=>1085,207=>1086,208=>1087,209=>1103,210=>1088,
211=>1089,212=>1090,213=>1091,214=>1078,215=>1074,216=>1100,
217=>1099,218=>1079,219=>1096,220=>1101,221=>1097,222=>1095,
223=>1098,224=>1070,225=>1040,226=>1041,227=>1062,228=>1044,
229=>1045,230=>1060,231=>1043,232=>1061,233=>1048,234=>1049,
235=>1050,236=>1051,237=>1052,238=>1053,239=>1054,240=>1055,
241=>1071,242=>1056,243=>1057,244=>1058,245=>1059,246=>1046,
247=>1042,248=>1068,249=>1067,250=>1047,251=>1064,252=>1069,
253=>1065,254=>1063,255=>1066),
"m"=>array(128=>1040,129=>1041,130=>1042,131=>1043,132=>1044,
133=>1045,134=>1046,135=>1047,136=>1048,137=>1049,138=>1050,
139=>1051,140=>1052,141=>1053,142=>1054,143=>1055,144=>1056,
145=>1057,146=>1058,147=>1059,148=>1060,149=>1061,150=>1062,
151=>1063,152=>1064,153=>1065,154=>1066,155=>1067,156=>1068,
157=>1069,158=>1070,159=>1071,160=>8224,161=>176,162=>1168,
163=>163,164=>167,165=>8226,166=>182,167=>1030,168=>174,
169=>169,170=>8482,171=>1026,172=>1106,173=>8800,174=>1027,
175=>1107,176=>8734,177=>177,178=>8804,179=>8805,180=>1110,
181=>181,182=>1169,183=>1032,184=>1028,185=>1108,186=>1031,
187=>1111,188=>1033,189=>1113,190=>1034,191=>1114,192=>1112,
193=>1029,194=>172,195=>8730,196=>402,197=>8776,198=>8710,
199=>171,200=>187,201=>8230,202=>160,203=>1035,204=>1115,
205=>1036,206=>1116,207=>1109,208=>8211,209=>8212,210=>8220,
211=>8221,212=>8216,213=>8217,214=>247,215=>8222,216=>1038,
217=>1118,218=>1039,219=>1119,220=>8470,221=>1025,222=>1105,
223=>1103,224=>1072,225=>1073,226=>1074,227=>1075,228=>1076,
229=>1077,230=>1078,231=>1079,232=>1080,233=>1081,234=>1082,
235=>1083,236=>1084,237=>1085,238=>1086,239=>1087,240=>1088,
241=>1089,242=>1090,243=>1091,244=>1092,245=>1093,246=>1094,
247=>1095,248=>1096,249=>1097,250=>1098,251=>1099,252=>1100,
253=>1101,254=>1102,255=>8364),
"w"=>array(128=>1026,129=>1027,130=>8218,131=>1107,132=>8222,
133=>8230,134=>8224,135=>8225,136=>8364,137=>8240,138=>1033,
139=>8249,140=>1034,141=>1036,142=>1035,143=>1039,144=>1106,
145=>8216,146=>8217,147=>8220,148=>8221,149=>8226,150=>8211,
151=>8212,153=>8482,154=>1113,155=>8250,156=>1114,157=>1116,
158=>1115,159=>1119,160=>160,161=>1038,162=>1118,163=>1032,
164=>164,165=>1168,166=>166,167=>167,168=>1025,169=>169,
170=>1028,171=>171,172=>172,173=>173,174=>174,175=>1031,
176=>176,177=>177,178=>1030,179=>1110,180=>1169,181=>181,
182=>182,183=>183,184=>1105,185=>8470,186=>1108,187=>187,
188=>1112,189=>1029,190=>1109,191=>1111,192=>1040,193=>1041,
194=>1042,195=>1043,196=>1044,197=>1045,198=>1046,199=>1047,
200=>1048,201=>1049,202=>1050,203=>1051,204=>1052,205=>1053,
206=>1054,207=>1055,208=>1056,209=>1057,210=>1058,211=>1059,
212=>1060,213=>1061,214=>1062,215=>1063,216=>1064,217=>1065,
218=>1066,219=>1067,220=>1068,221=>1069,222=>1070,223=>1071,
224=>1072,225=>1073,226=>1074,227=>1075,228=>1076,229=>1077,
230=>1078,231=>1079,232=>1080,233=>1081,234=>1082,235=>1083,
236=>1084,237=>1085,238=>1086,239=>1087,240=>1088,241=>1089,
242=>1090,243=>1091,244=>1092,245=>1093,246=>1094,247=>1095,
248=>1096,249=>1097,250=>1098,251=>1099,252=>1100,253=>1101,
254=>1102,255=>1103),
);
$this->rmap = array(
"d"=>array(1040=>128,1041=>129,1042=>130,1043=>131,1044=>132,
1045=>133,1046=>134,1047=>135,1048=>136,1049=>137,1050=>138,
1051=>139,1052=>140,1053=>141,1054=>142,1055=>143,1056=>144,
1057=>145,1058=>146,1059=>147,1060=>148,1061=>149,1062=>150,
1063=>151,1064=>152,1065=>153,1066=>154,1067=>155,1068=>156,
1069=>157,1070=>158,1071=>159,1072=>160,1073=>161,1074=>162,
1075=>163,1076=>164,1077=>165,1078=>166,1079=>167,1080=>168,
1081=>169,1082=>170,1083=>171,1084=>172,1085=>173,1086=>174,
1087=>175,9617=>176,9618=>177,9619=>178,9474=>179,9508=>180,
9569=>181,9570=>182,9558=>183,9557=>184,9571=>185,9553=>186,
9559=>187,9565=>188,9564=>189,9563=>190,9488=>191,9492=>192,
9524=>193,9516=>194,9500=>195,9472=>196,9532=>197,9566=>198,
9567=>199,9562=>200,9556=>201,9577=>202,9574=>203,9568=>204,
9552=>205,9580=>206,9575=>207,9576=>208,9572=>209,9573=>210,
9561=>211,9560=>212,9554=>213,9555=>214,9579=>215,9578=>216,
9496=>217,9484=>218,9608=>219,9604=>220,9612=>221,9616=>222,
9600=>223,1088=>224,1089=>225,1090=>226,1091=>227,1092=>228,
1093=>229,1094=>230,1095=>231,1096=>232,1097=>233,1098=>234,
1099=>235,1100=>236,1101=>237,1102=>238,1103=>239,1025=>240,
1105=>241,1028=>242,1108=>243,1031=>244,1111=>245,1038=>246,
1118=>247,176=>248,8729=>249,183=>250,8730=>251,8470=>252,
164=>253,9632=>254,160=>255),
"i"=>array(128=>128,129=>129,130=>130,131=>131,132=>132,133=>133,
134=>134,135=>135,136=>136,137=>137,138=>138,139=>139,140=>140,
141=>141,142=>142,143=>143,144=>144,145=>145,146=>146,147=>147,
148=>148,149=>149,150=>150,151=>151,152=>152,153=>153,154=>154,
155=>155,156=>156,157=>157,158=>158,159=>159,160=>160,1025=>161,
1026=>162,1027=>163,1028=>164,1029=>165,1030=>166,1031=>167,
1032=>168,1033=>169,1034=>170,1035=>171,1036=>172,173=>173,
1038=>174,1039=>175,1040=>176,1041=>177,1042=>178,1043=>179,
1044=>180,1045=>181,1046=>182,1047=>183,1048=>184,1049=>185,
1050=>186,1051=>187,1052=>188,1053=>189,1054=>190,1055=>191,
1056=>192,1057=>193,1058=>194,1059=>195,1060=>196,1061=>197,
1062=>198,1063=>199,1064=>200,1065=>201,1066=>202,1067=>203,
1068=>204,1069=>205,1070=>206,1071=>207,1072=>208,1073=>209,
1074=>210,1075=>211,1076=>212,1077=>213,1078=>214,1079=>215,
1080=>216,1081=>217,1082=>218,1083=>219,1084=>220,1085=>221,
1086=>222,1087=>223,1088=>224,1089=>225,1090=>226,1091=>227,
1092=>228,1093=>229,1094=>230,1095=>231,1096=>232,1097=>233,
1098=>234,1099=>235,1100=>236,1101=>237,1102=>238,1103=>239,
8470=>240,1105=>241,1106=>242,1107=>243,1108=>244,1109=>245,
1110=>246,1111=>247,1112=>248,1113=>249,1114=>250,1115=>251,
1116=>252,167=>253,1118=>254,1119=>255),
"k"=>array(9472=>128,9474=>129,9484=>130,9488=>131,9492=>132,
9496=>133,9500=>134,9508=>135,9516=>136,9524=>137,9532=>138,
9600=>139,9604=>140,9608=>141,9612=>142,9616=>143,9617=>144,
9618=>145,9619=>146,8992=>147,9632=>148,8729=>149,8730=>150,
8776=>151,8804=>152,8805=>153,160=>154,8993=>155,176=>156,
178=>157,183=>158,247=>159,9552=>160,9553=>161,9554=>162,
1105=>163,9555=>164,9556=>165,9557=>166,9558=>167,9559=>168,
9560=>169,9561=>170,9562=>171,9563=>172,9564=>173,9565=>174,
9566=>175,9567=>176,9568=>177,9569=>178,1025=>179,9570=>180,
9571=>181,9572=>182,9573=>183,9574=>184,9575=>185,9576=>186,
9577=>187,9578=>188,9579=>189,9580=>190,169=>191,1102=>192,
1072=>193,1073=>194,1094=>195,1076=>196,1077=>197,1092=>198,
1075=>199,1093=>200,1080=>201,1081=>202,1082=>203,1083=>204,
1084=>205,1085=>206,1086=>207,1087=>208,1103=>209,1088=>210,
1089=>211,1090=>212,1091=>213,1078=>214,1074=>215,1100=>216,
1099=>217,1079=>218,1096=>219,1101=>220,1097=>221,1095=>222,
1098=>223,1070=>224,1040=>225,1041=>226,1062=>227,1044=>228,
1045=>229,1060=>230,1043=>231,1061=>232,1048=>233,1049=>234,
1050=>235,1051=>236,1052=>237,1053=>238,1054=>239,1055=>240,
1071=>241,1056=>242,1057=>243,1058=>244,1059=>245,1046=>246,
1042=>247,1068=>248,1067=>249,1047=>250,1064=>251,1069=>252,
1065=>253,1063=>254,1066=>255),
"m"=>array(1040=>128,1041=>129,1042=>130,1043=>131,1044=>132,
1045=>133,1046=>134,1047=>135,1048=>136,1049=>137,1050=>138,
1051=>139,1052=>140,1053=>141,1054=>142,1055=>143,1056=>144,
1057=>145,1058=>146,1059=>147,1060=>148,1061=>149,1062=>150,
1063=>151,1064=>152,1065=>153,1066=>154,1067=>155,1068=>156,
1069=>157,1070=>158,1071=>159,8224=>160,176=>161,1168=>162,
163=>163,167=>164,8226=>165,182=>166,1030=>167,174=>168,
169=>169,8482=>170,1026=>171,1106=>172,8800=>173,1027=>174,
1107=>175,8734=>176,177=>177,8804=>178,8805=>179,1110=>180,
181=>181,1169=>182,1032=>183,1028=>184,1108=>185,1031=>186,
1111=>187,1033=>188,1113=>189,1034=>190,1114=>191,1112=>192,
1029=>193,172=>194,8730=>195,402=>196,8776=>197,8710=>198,
171=>199,187=>200,8230=>201,160=>202,1035=>203,1115=>204,
1036=>205,1116=>206,1109=>207,8211=>208,8212=>209,8220=>210,
8221=>211,8216=>212,8217=>213,247=>214,8222=>215,1038=>216,
1118=>217,1039=>218,1119=>219,8470=>220,1025=>221,1105=>222,
1103=>223,1072=>224,1073=>225,1074=>226,1075=>227,1076=>228,
1077=>229,1078=>230,1079=>231,1080=>232,1081=>233,1082=>234,
1083=>235,1084=>236,1085=>237,1086=>238,1087=>239,1088=>240,
1089=>241,1090=>242,1091=>243,1092=>244,1093=>245,1094=>246,
1095=>247,1096=>248,1097=>249,1098=>250,1099=>251,1100=>252,
1101=>253,1102=>254,8364=>255),
"w"=>array(1026=>128,1027=>129,8218=>130,1107=>131,8222=>132,
8230=>133,8224=>134,8225=>135,8364=>136,8240=>137,1033=>138,
8249=>139,1034=>140,1036=>141,1035=>142,1039=>143,1106=>144,
8216=>145,8217=>146,8220=>147,8221=>148,8226=>149,8211=>150,
8212=>151,8482=>153,1113=>154,8250=>155,1114=>156,1116=>157,
1115=>158,1119=>159,160=>160,1038=>161,1118=>162,1032=>163,
164=>164,1168=>165,166=>166,167=>167,1025=>168,169=>169,
1028=>170,171=>171,172=>172,173=>173,174=>174,1031=>175,
176=>176,177=>177,1030=>178,1110=>179,1169=>180,181=>181,
182=>182,183=>183,1105=>184,8470=>185,1108=>186,187=>187,
1112=>188,1029=>189,1109=>190,1111=>191,1040=>192,1041=>193,
1042=>194,1043=>195,1044=>196,1045=>197,1046=>198,1047=>199,
1048=>200,1049=>201,1050=>202,1051=>203,1052=>204,1053=>205,
1054=>206,1055=>207,1056=>208,1057=>209,1058=>210,1059=>211,
1060=>212,1061=>213,1062=>214,1063=>215,1064=>216,1065=>217,
1066=>218,1067=>219,1068=>220,1069=>221,1070=>222,1071=>223,
1072=>224,1073=>225,1074=>226,1075=>227,1076=>228,1077=>229,
1078=>230,1079=>231,1080=>232,1081=>233,1082=>234,1083=>235,
1084=>236,1085=>237,1086=>238,1087=>239,1088=>240,1089=>241,
1090=>242,1091=>243,1092=>244,1093=>245,1094=>246,1095=>247,
1096=>248,1097=>249,1098=>250,1099=>251,1100=>252,1101=>253,
1102=>254,1103=>255),
);
}
function convert($str, $from, $to) {
if ($from == 'a') $from = 'd';
if ($to == 'a') $to = 'd';
# convert to unicode
if ($to == 'u') {
if ($from == 'u') {
return $str;
} else {
$n = strlen($str);
for ($i=0;$i<$n;$i++) {
$c=ord(substr($str,$i,1));
if ($c < 0x80) {
$xstr .= chr($c);
} else {
$xstr .= $this->code2utf($this->map[$from][$c]);
}
}
return $xstr;
}
}
# convert from unicode
if ($from == 'u') {
if ($to == 'u') {
return $str;
} else {
$n = strlen($str);
do {
$c = ord(substr($str,$i,1));
if ($c<0x80) {
$i++;
$xstr .= chr($c);
} else {
if (($c & 0xE0) == 0xC0) {
$c1 = ($c & 0x1F) << 6;
$c2 = ord(substr($str,$i+1,1)) & 0x3F;
$c = $c1 + $c2;
$i+=2;
} elseif (($c & 0xF0) == 0xE0) {
$c1 = ($c & 0x0F) << 12;
$c2 = (ord(substr($str,$i+1,1)) & 0x3F) << 6;
$c3 = ord(substr($str,$i+2,1)) & 0x3F;
$c = $c1 + $c2 + $c3;
$i+=3;
} else {
// impossible
$c = 0;
$i+=4;
}
$xstr .= chr($this->rmap[$to][$c]);
}
} while ($i<$n);
return $xstr;
}
}
# otherwise
$n = strlen($str);
for ($i=0;$i<$n;$i++) {
$c=ord(substr($str,$i,1));
if ($c < 0x80) {
$xstr .= chr($c);
} else {
if (!$c = $this->rmap[$to][$this->map[$from][$c]]) $c=32;
$xstr .= chr($c);
}
}
return $xstr;
}
function code2utf($num) {
# Translates numeric code of UTF-8 character code to
#corresponding character sequence. Refer to www.unicode.org for info.
if ($num<0x80) return chr($num); // ASCII7
if ($num<0x800) return chr(($num>>6)+192).chr(($num&63)+128);
if ($num<0x10000) return chr(($num>>12)+240).chr((($num>>6)&63)+128).chr(($num&63)+128);
return chr($num>>18+240).chr((($num>>12)&63)+128).chr(($num>>6)&63+128).chr($num&63+128);
}
}
?>