cp1256.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * cp1256 encoding functions
  4. *
  5. * takes a string of unicode entities and converts it to a cp1256 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 cp1256
  15. * @param string $string text with numeric unicode entities
  16. * @return string cp1256 encoded text
  17. */
  18. function charset_encode_cp1256 ($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","unicodetocp1256('\\1')",$string);
  22. // $string=preg_replace("/&#[xX]([0-9A-F]+);/e","unicodetocp1256(hexdec('\\1'))",$string);
  23. return $string;
  24. }
  25. /**
  26. * Return cp1256 symbol when unicode character number is provided
  27. *
  28. * This function is used internally by charset_encode_cp1256
  29. * function. It might be unavailable to other SquirrelMail functions.
  30. * Don't use it or make sure, that functions/encode/cp1256.php is
  31. * included.
  32. *
  33. * @param int $var decimal unicode value
  34. * @return string cp1256 character
  35. */
  36. function unicodetocp1256($var) {
  37. $cp1256chars=array('160' => "\xA0",
  38. '162' => "\xA2",
  39. '163' => "\xA3",
  40. '164' => "\xA4",
  41. '165' => "\xA5",
  42. '166' => "\xA6",
  43. '167' => "\xA7",
  44. '168' => "\xA8",
  45. '169' => "\xA9",
  46. '171' => "\xAB",
  47. '172' => "\xAC",
  48. '173' => "\xAD",
  49. '174' => "\xAE",
  50. '175' => "\xAF",
  51. '176' => "\xB0",
  52. '177' => "\xB1",
  53. '178' => "\xB2",
  54. '179' => "\xB3",
  55. '180' => "\xB4",
  56. '181' => "\xB5",
  57. '182' => "\xB6",
  58. '183' => "\xB7",
  59. '184' => "\xB8",
  60. '185' => "\xB9",
  61. '187' => "\xBB",
  62. '188' => "\xBC",
  63. '189' => "\xBD",
  64. '190' => "\xBE",
  65. '215' => "\xD7",
  66. '224' => "\xE0",
  67. '226' => "\xE2",
  68. '231' => "\xE7",
  69. '232' => "\xE8",
  70. '233' => "\xE9",
  71. '234' => "\xEA",
  72. '235' => "\xEB",
  73. '238' => "\xEE",
  74. '239' => "\xEF",
  75. '244' => "\xF4",
  76. '247' => "\xF7",
  77. '249' => "\xF9",
  78. '251' => "\xFB",
  79. '252' => "\xFC",
  80. '338' => "\x8C",
  81. '339' => "\x9C",
  82. '402' => "\x83",
  83. '710' => "\x88",
  84. '1548' => "\xA1",
  85. '1563' => "\xBA",
  86. '1567' => "\xBF",
  87. '1569' => "\xC1",
  88. '1570' => "\xC2",
  89. '1571' => "\xC3",
  90. '1572' => "\xC4",
  91. '1573' => "\xC5",
  92. '1574' => "\xC6",
  93. '1575' => "\xC7",
  94. '1576' => "\xC8",
  95. '1577' => "\xC9",
  96. '1578' => "\xCA",
  97. '1579' => "\xCB",
  98. '1580' => "\xCC",
  99. '1581' => "\xCD",
  100. '1582' => "\xCE",
  101. '1583' => "\xCF",
  102. '1584' => "\xD0",
  103. '1585' => "\xD1",
  104. '1586' => "\xD2",
  105. '1587' => "\xD3",
  106. '1588' => "\xD4",
  107. '1589' => "\xD5",
  108. '1590' => "\xD6",
  109. '1591' => "\xD8",
  110. '1592' => "\xD9",
  111. '1593' => "\xDA",
  112. '1594' => "\xDB",
  113. '1600' => "\xDC",
  114. '1601' => "\xDD",
  115. '1602' => "\xDE",
  116. '1603' => "\xDF",
  117. '1604' => "\xE1",
  118. '1605' => "\xE3",
  119. '1606' => "\xE4",
  120. '1607' => "\xE5",
  121. '1608' => "\xE6",
  122. '1609' => "\xEC",
  123. '1610' => "\xED",
  124. '1611' => "\xF0",
  125. '1612' => "\xF1",
  126. '1613' => "\xF2",
  127. '1614' => "\xF3",
  128. '1615' => "\xF5",
  129. '1616' => "\xF6",
  130. '1617' => "\xF8",
  131. '1618' => "\xFA",
  132. '1657' => "\x8A",
  133. '1662' => "\x81",
  134. '1670' => "\x8D",
  135. '1672' => "\x8F",
  136. '1681' => "\x9A",
  137. '1688' => "\x8E",
  138. '1705' => "\x98",
  139. '1711' => "\x90",
  140. '1722' => "\x9F",
  141. '1726' => "\xAA",
  142. '1729' => "\xC0",
  143. '1746' => "\xFF",
  144. '8204' => "\x9D",
  145. '8205' => "\x9E",
  146. '8206' => "\xFD",
  147. '8207' => "\xFE",
  148. '8211' => "\x96",
  149. '8212' => "\x97",
  150. '8216' => "\x91",
  151. '8217' => "\x92",
  152. '8218' => "\x82",
  153. '8220' => "\x93",
  154. '8221' => "\x94",
  155. '8222' => "\x84",
  156. '8224' => "\x86",
  157. '8225' => "\x87",
  158. '8226' => "\x95",
  159. '8230' => "\x85",
  160. '8240' => "\x89",
  161. '8249' => "\x8B",
  162. '8250' => "\x9B",
  163. '8364' => "\x80",
  164. '8482' => "\x99");
  165. if (array_key_exists($var,$cp1256chars)) {
  166. $ret=$cp1256chars[$var];
  167. } else {
  168. $ret='?';
  169. }
  170. return $ret;
  171. }
  172. ?>