cp1251.php 6.4 KB

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