CodePage.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. class CodePage
  5. {
  6. public const DEFAULT_CODE_PAGE = 'CP1252';
  7. private static $pageArray = [
  8. 0 => 'CP1252', // CodePage is not always correctly set when the xls file was saved by Apple's Numbers program
  9. 367 => 'ASCII', // ASCII
  10. 437 => 'CP437', // OEM US
  11. //720 => 'notsupported', // OEM Arabic
  12. 737 => 'CP737', // OEM Greek
  13. 775 => 'CP775', // OEM Baltic
  14. 850 => 'CP850', // OEM Latin I
  15. 852 => 'CP852', // OEM Latin II (Central European)
  16. 855 => 'CP855', // OEM Cyrillic
  17. 857 => 'CP857', // OEM Turkish
  18. 858 => 'CP858', // OEM Multilingual Latin I with Euro
  19. 860 => 'CP860', // OEM Portugese
  20. 861 => 'CP861', // OEM Icelandic
  21. 862 => 'CP862', // OEM Hebrew
  22. 863 => 'CP863', // OEM Canadian (French)
  23. 864 => 'CP864', // OEM Arabic
  24. 865 => 'CP865', // OEM Nordic
  25. 866 => 'CP866', // OEM Cyrillic (Russian)
  26. 869 => 'CP869', // OEM Greek (Modern)
  27. 874 => 'CP874', // ANSI Thai
  28. 932 => 'CP932', // ANSI Japanese Shift-JIS
  29. 936 => 'CP936', // ANSI Chinese Simplified GBK
  30. 949 => 'CP949', // ANSI Korean (Wansung)
  31. 950 => 'CP950', // ANSI Chinese Traditional BIG5
  32. 1200 => 'UTF-16LE', // UTF-16 (BIFF8)
  33. 1250 => 'CP1250', // ANSI Latin II (Central European)
  34. 1251 => 'CP1251', // ANSI Cyrillic
  35. 1252 => 'CP1252', // ANSI Latin I (BIFF4-BIFF7)
  36. 1253 => 'CP1253', // ANSI Greek
  37. 1254 => 'CP1254', // ANSI Turkish
  38. 1255 => 'CP1255', // ANSI Hebrew
  39. 1256 => 'CP1256', // ANSI Arabic
  40. 1257 => 'CP1257', // ANSI Baltic
  41. 1258 => 'CP1258', // ANSI Vietnamese
  42. 1361 => 'CP1361', // ANSI Korean (Johab)
  43. 10000 => 'MAC', // Apple Roman
  44. 10001 => 'CP932', // Macintosh Japanese
  45. 10002 => 'CP950', // Macintosh Chinese Traditional
  46. 10003 => 'CP1361', // Macintosh Korean
  47. 10004 => 'MACARABIC', // Apple Arabic
  48. 10005 => 'MACHEBREW', // Apple Hebrew
  49. 10006 => 'MACGREEK', // Macintosh Greek
  50. 10007 => 'MACCYRILLIC', // Macintosh Cyrillic
  51. 10008 => 'CP936', // Macintosh - Simplified Chinese (GB 2312)
  52. 10010 => 'MACROMANIA', // Macintosh Romania
  53. 10017 => 'MACUKRAINE', // Macintosh Ukraine
  54. 10021 => 'MACTHAI', // Macintosh Thai
  55. 10029 => 'MACCENTRALEUROPE', // Macintosh Central Europe
  56. 10079 => 'MACICELAND', // Macintosh Icelandic
  57. 10081 => 'MACTURKISH', // Macintosh Turkish
  58. 10082 => 'MACCROATIAN', // Macintosh Croatian
  59. 21010 => 'UTF-16LE', // UTF-16 (BIFF8) This isn't correct, but some Excel writer libraries erroneously use Codepage 21010 for UTF-16LE
  60. 32768 => 'MAC', // Apple Roman
  61. //32769 => 'unsupported', // ANSI Latin I (BIFF2-BIFF3)
  62. 65000 => 'UTF-7', // Unicode (UTF-7)
  63. 65001 => 'UTF-8', // Unicode (UTF-8)
  64. ];
  65. public static function validate(string $codePage): bool
  66. {
  67. return in_array($codePage, self::$pageArray, true);
  68. }
  69. /**
  70. * Convert Microsoft Code Page Identifier to Code Page Name which iconv
  71. * and mbstring understands.
  72. *
  73. * @param int $codePage Microsoft Code Page Indentifier
  74. *
  75. * @return string Code Page Name
  76. */
  77. public static function numberToName(int $codePage): string
  78. {
  79. if (array_key_exists($codePage, self::$pageArray)) {
  80. return self::$pageArray[$codePage];
  81. }
  82. if ($codePage == 720 || $codePage == 32769) {
  83. throw new PhpSpreadsheetException("Code page $codePage not supported."); // OEM Arabic
  84. }
  85. throw new PhpSpreadsheetException('Unknown codepage: ' . $codePage);
  86. }
  87. public static function getEncodings(): array
  88. {
  89. return self::$pageArray;
  90. }
  91. }