| 1 | <?php |
| 2 | /** |
| 3 | * koi8-r encoding functions |
| 4 | * |
| 5 | * takes a string of unicode entities and converts it to a koi8-r encoded string |
| 6 | * Unsupported characters are replaced with ?. |
| 7 | * |
| 8 | * @version $Id$ |
| 9 | * @copyright Copyright © 2004-2005 The SquirrelMail Project Team |
| 10 | * @package squirrelmail |
| 11 | * @subpackage encode |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Converts string to koi8-r |
| 16 | * @param string $string text with numeric unicode entities |
| 17 | * @return string koi8-r encoded text |
| 18 | */ |
| 19 | function charset_encode_koi8_r ($string) { |
| 20 | // don't run encoding function, if there is no encoded characters |
| 21 | if (! preg_match("'&#[0-9]+;'",$string) ) return $string; |
| 22 | |
| 23 | $string=preg_replace("/&#([0-9]+);/e","unicodetokoi8r('\\1')",$string); |
| 24 | // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetokoi8r(hexdec('\\1'))",$string); |
| 25 | |
| 26 | return $string; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return koi8-r symbol when unicode character number is provided |
| 31 | * |
| 32 | * This function is used internally by charset_encode_koi8_r |
| 33 | * function. It might be unavailable to other SquirrelMail functions. |
| 34 | * Don't use it or make sure, that functions/encode/koi8_r.php is |
| 35 | * included. |
| 36 | * |
| 37 | * @param int $var decimal unicode value |
| 38 | * @return string koi8-r character |
| 39 | */ |
| 40 | function unicodetokoi8r($var) { |
| 41 | |
| 42 | $koi8rchars=array('160' => "\x9A", |
| 43 | '169' => "\xBF", |
| 44 | '176' => "\x9C", |
| 45 | '178' => "\x9D", |
| 46 | '183' => "\x9E", |
| 47 | '247' => "\x9F", |
| 48 | '1025' => "\xB3", |
| 49 | '1040' => "\xE1", |
| 50 | '1041' => "\xE2", |
| 51 | '1042' => "\xF7", |
| 52 | '1043' => "\xE7", |
| 53 | '1044' => "\xE4", |
| 54 | '1045' => "\xE5", |
| 55 | '1046' => "\xF6", |
| 56 | '1047' => "\xFA", |
| 57 | '1048' => "\xE9", |
| 58 | '1049' => "\xEA", |
| 59 | '1050' => "\xEB", |
| 60 | '1051' => "\xEC", |
| 61 | '1052' => "\xED", |
| 62 | '1053' => "\xEE", |
| 63 | '1054' => "\xEF", |
| 64 | '1055' => "\xF0", |
| 65 | '1056' => "\xF2", |
| 66 | '1057' => "\xF3", |
| 67 | '1058' => "\xF4", |
| 68 | '1059' => "\xF5", |
| 69 | '1060' => "\xE6", |
| 70 | '1061' => "\xE8", |
| 71 | '1062' => "\xE3", |
| 72 | '1063' => "\xFE", |
| 73 | '1064' => "\xFB", |
| 74 | '1065' => "\xFD", |
| 75 | '1066' => "\xFF", |
| 76 | '1067' => "\xF9", |
| 77 | '1068' => "\xF8", |
| 78 | '1069' => "\xFC", |
| 79 | '1070' => "\xE0", |
| 80 | '1071' => "\xF1", |
| 81 | '1072' => "\xC1", |
| 82 | '1073' => "\xC2", |
| 83 | '1074' => "\xD7", |
| 84 | '1075' => "\xC7", |
| 85 | '1076' => "\xC4", |
| 86 | '1077' => "\xC5", |
| 87 | '1078' => "\xD6", |
| 88 | '1079' => "\xDA", |
| 89 | '1080' => "\xC9", |
| 90 | '1081' => "\xCA", |
| 91 | '1082' => "\xCB", |
| 92 | '1083' => "\xCC", |
| 93 | '1084' => "\xCD", |
| 94 | '1085' => "\xCE", |
| 95 | '1086' => "\xCF", |
| 96 | '1087' => "\xD0", |
| 97 | '1088' => "\xD2", |
| 98 | '1089' => "\xD3", |
| 99 | '1090' => "\xD4", |
| 100 | '1091' => "\xD5", |
| 101 | '1092' => "\xC6", |
| 102 | '1093' => "\xC8", |
| 103 | '1094' => "\xC3", |
| 104 | '1095' => "\xDE", |
| 105 | '1096' => "\xDB", |
| 106 | '1097' => "\xDD", |
| 107 | '1098' => "\xDF", |
| 108 | '1099' => "\xD9", |
| 109 | '1100' => "\xD8", |
| 110 | '1101' => "\xDC", |
| 111 | '1102' => "\xC0", |
| 112 | '1103' => "\xD1", |
| 113 | '1105' => "\xA3", |
| 114 | '8729' => "\x95", |
| 115 | '8730' => "\x96", |
| 116 | '8776' => "\x97", |
| 117 | '8804' => "\x98", |
| 118 | '8805' => "\x99", |
| 119 | '8992' => "\x93", |
| 120 | '8993' => "\x9B", |
| 121 | '9472' => "\x80", |
| 122 | '9474' => "\x81", |
| 123 | '9484' => "\x82", |
| 124 | '9488' => "\x83", |
| 125 | '9492' => "\x84", |
| 126 | '9496' => "\x85", |
| 127 | '9500' => "\x86", |
| 128 | '9508' => "\x87", |
| 129 | '9516' => "\x88", |
| 130 | '9524' => "\x89", |
| 131 | '9532' => "\x8A", |
| 132 | '9552' => "\xA0", |
| 133 | '9553' => "\xA1", |
| 134 | '9554' => "\xA2", |
| 135 | '9555' => "\xA4", |
| 136 | '9556' => "\xA5", |
| 137 | '9557' => "\xA6", |
| 138 | '9558' => "\xA7", |
| 139 | '9559' => "\xA8", |
| 140 | '9560' => "\xA9", |
| 141 | '9561' => "\xAA", |
| 142 | '9562' => "\xAB", |
| 143 | '9563' => "\xAC", |
| 144 | '9564' => "\xAD", |
| 145 | '9565' => "\xAE", |
| 146 | '9566' => "\xAF", |
| 147 | '9567' => "\xB0", |
| 148 | '9568' => "\xB1", |
| 149 | '9569' => "\xB2", |
| 150 | '9570' => "\xB4", |
| 151 | '9571' => "\xB5", |
| 152 | '9572' => "\xB6", |
| 153 | '9573' => "\xB7", |
| 154 | '9574' => "\xB8", |
| 155 | '9575' => "\xB9", |
| 156 | '9576' => "\xBA", |
| 157 | '9577' => "\xBB", |
| 158 | '9578' => "\xBC", |
| 159 | '9579' => "\xBD", |
| 160 | '9580' => "\xBE", |
| 161 | '9600' => "\x8B", |
| 162 | '9604' => "\x8C", |
| 163 | '9608' => "\x8D", |
| 164 | '9612' => "\x8E", |
| 165 | '9616' => "\x8F", |
| 166 | '9617' => "\x90", |
| 167 | '9618' => "\x91", |
| 168 | '9619' => "\x92", |
| 169 | '9632' => "\x94"); |
| 170 | |
| 171 | if (array_key_exists($var,$koi8rchars)) { |
| 172 | $ret=$koi8rchars[$var]; |
| 173 | } else { |
| 174 | $ret='?'; |
| 175 | } |
| 176 | return $ret; |
| 177 | } |
| 178 | ?> |