mb_detect_encoding.phpt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. --TEST--
  2. mb_detect_encoding()
  3. --SKIPIF--
  4. <?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
  5. --INI--
  6. mbstring.language=Japanese
  7. --FILE--
  8. <?php
  9. // TODO: Add more tests
  10. //$debug = true; // Uncomment this line to view error/warning/notice message in *.out file
  11. ini_set('include_path', dirname(__FILE__));
  12. include_once('common.inc');
  13. // SJIS string (BASE64 encoded)
  14. $sjis = base64_decode('k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==');
  15. // JIS string (BASE64 encoded)
  16. $jis = base64_decode('GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==');
  17. // EUC-JP string
  18. $euc_jp = '日本語テキストです。0123456789。';
  19. // Test with sigle "form encoding"
  20. // Note: For some reason it complains, results are differ. Not reserched.
  21. echo "== BASIC TEST ==\n";
  22. $s = $sjis;
  23. $s = mb_detect_encoding($s, 'SJIS');
  24. print("SJIS: $s\n");
  25. $s = $jis;
  26. $s = mb_detect_encoding($s, 'JIS');
  27. print("JIS: $s\n");
  28. $s = $euc_jp;
  29. $s = mb_detect_encoding($s, 'UTF-8,EUC-JP,JIS');
  30. print("EUC-JP: $s\n");
  31. $s = $euc_jp;
  32. $s = mb_detect_encoding($s, 'JIS,EUC-JP');
  33. print("EUC-JP: $s\n");
  34. // Using Encoding List Array
  35. echo "== ARRAY ENCODING LIST ==\n";
  36. $a = array(0=>'UTF-8',1=>'EUC-JP', 2=>'SJIS', 3=>'JIS');
  37. // Note: Due to detect order, detected as UTF-8
  38. $s = $jis;
  39. $s = mb_detect_encoding($s, $a);
  40. print("JIS: $s\n");
  41. $s = $euc_jp;
  42. $s = mb_detect_encoding($s, $a);
  43. print("EUC-JP: $s\n");
  44. $s = $sjis;
  45. $s = mb_detect_encoding($s, $a);
  46. print("SJIS: $s\n");
  47. // Using Detect Order
  48. echo "== DETECT ORDER ==\n";
  49. mb_detect_order('auto');
  50. $s = $jis;
  51. $s = mb_detect_encoding($s);
  52. print("JIS: $s\n");
  53. $s = $euc_jp;
  54. $s = mb_detect_encoding($s);
  55. print("EUC-JP: $s\n");
  56. $s = $sjis;
  57. $s = mb_detect_encoding($s);
  58. print("SJIS: $s\n");
  59. // Invalid(?) Parameters
  60. echo "== INVALID PARAMETER ==\n";
  61. $s = mb_detect_encoding(1234, 'EUC-JP');
  62. print("INT: $s\n"); // EUC-JP
  63. $s = mb_detect_encoding('', 'EUC-JP');
  64. print("EUC-JP: $s\n"); // SJIS
  65. $s = $euc_jp;
  66. $s = mb_detect_encoding($s, 'BAD');
  67. print("BAD: $s\n"); // BAD
  68. $s = $euc_jp;
  69. $s = mb_detect_encoding();
  70. print("MP: $s\n"); // Missing parameter
  71. ?>
  72. --EXPECT--
  73. == BASIC TEST ==
  74. SJIS: SJIS
  75. JIS: JIS
  76. EUC-JP: EUC-JP
  77. EUC-JP: EUC-JP
  78. == ARRAY ENCODING LIST ==
  79. JIS: UTF-8
  80. EUC-JP: EUC-JP
  81. SJIS: SJIS
  82. == DETECT ORDER ==
  83. JIS: JIS
  84. EUC-JP: EUC-JP
  85. SJIS: SJIS
  86. == INVALID PARAMETER ==
  87. INT: EUC-JP
  88. EUC-JP: EUC-JP
  89. ERR: Warning
  90. BAD: EUC-JP
  91. ERR: Warning
  92. MP: