cp1251.php 6.4 KB

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