mb_convert_encoding.phpt 3.6 KB

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