1--TEST--
2mb_convert_encoding()
3--SKIPIF--
4<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
5--INI--
6output_handler=
7mbstring.language=Japanese
8--FILE--
9<?php
10// TODO: Add more tests
11
12// SJIS string (BASE64 encoded)
13$sjis = base64_decode('k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==');
14// JIS string (BASE64 encoded)
15$jis = base64_decode('GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==');
16// EUC-JP string
17$euc_jp = '���ܸ�ƥ����ȤǤ���01234������������';
18
19// Test with single "form encoding"
20// Note: For some reason it complains, results are different. Not researched.
21echo "== BASIC TEST ==\n";
22$s = $sjis;
23$s = mb_convert_encoding($s, 'EUC-JP', 'SJIS');
24print("EUC-JP: $s\n"); // EUC-JP
25
26$s = $jis;
27$s = mb_convert_encoding($s, 'EUC-JP', 'JIS');
28print("EUC-JP: $s\n"); // EUC-JP
29
30$s = $euc_jp;
31$s = mb_convert_encoding($s, 'SJIS', 'EUC-JP');
32print("SJIS: ".base64_encode($s)."\n"); // SJIS
33
34$s = $euc_jp;
35$s = mb_convert_encoding($s, 'JIS', 'EUC-JP');
36print("JIS: ".base64_encode($s)."\n"); // JIS
37
38
39// Using Encoding List Array
40echo "== STRING ENCODING LIST ==\n";
41
42$a = 'JIS,UTF-8,EUC-JP,SJIS';
43$s = $jis;
44$s = mb_convert_encoding($s, 'EUC-JP', $a);
45print("EUC-JP: $s\n"); // EUC-JP
46
47$s = $euc_jp;
48$s = mb_convert_encoding($s, 'SJIS', $a);
49print("SJIS: ".base64_encode($s)."\n");  // SJIS
50
51$s = $euc_jp;
52$s = mb_convert_encoding($s, 'JIS', $a);
53print("JIS: ".base64_encode($s)."\n"); // JIS
54
55
56// Using Encoding List Array
57echo "== ARRAY ENCODING LIST ==\n";
58
59$a = array(0=>'JIS', 1=>'UTF-8', 2=>'EUC-JP', 3=>'SJIS');
60$s = $jis;
61$s = mb_convert_encoding($s, 'EUC-JP', $a);
62print("EUC-JP: $s\n"); // EUC-JP
63
64$s = $euc_jp;
65$s = mb_convert_encoding($s, 'SJIS', $a);
66print("SJIS: ".base64_encode($s)."\n");  // SJIS
67
68$s = $euc_jp;
69$s = mb_convert_encoding($s, 'JIS', $a);
70print("JIS: ".base64_encode($s)."\n"); // JIS
71
72
73// Using Detect Order
74echo "== DETECT ORDER ==\n";
75
76$s = $jis;
77$s = mb_convert_encoding($s, 'EUC-JP', 'auto');
78print("EUC-JP: $s\n"); // EUC-JP
79
80$s = $euc_jp;
81$s = mb_convert_encoding($s, 'SJIS', 'auto');
82print("SJIS: ".base64_encode($s)."\n");  // SJIS
83
84$s = $euc_jp;
85$s = mb_convert_encoding($s, 'JIS', 'auto');
86print("JIS: ".base64_encode($s)."\n"); // JIS
87
88
89// Invalid(?) Parameters
90echo "== INVALID PARAMETER ==\n";
91
92$s = mb_convert_encoding(1234, 'EUC-JP');
93print("INT: $s\n"); // EUC-JP
94
95$s = mb_convert_encoding('', 'EUC-JP');
96print("EUC-JP: $s\n");  // SJIS
97
98$s = $euc_jp;
99try {
100    var_dump( mb_convert_encoding($s, 'BAD') );
101} catch (\ValueError $e) {
102    echo $e->getMessage() . \PHP_EOL;
103}
104
105?>
106--EXPECT--
107== BASIC TEST ==
108EUC-JP: ���ܸ�ƥ����ȤǤ���01234������������
109EUC-JP: ���ܸ�ƥ����ȤǤ���01234������������
110SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
111JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
112== STRING ENCODING LIST ==
113EUC-JP: ���ܸ�ƥ����ȤǤ���01234������������
114SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
115JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
116== ARRAY ENCODING LIST ==
117EUC-JP: ���ܸ�ƥ����ȤǤ���01234������������
118SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
119JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
120== DETECT ORDER ==
121EUC-JP: ���ܸ�ƥ����ȤǤ���01234������������
122SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
123JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
124== INVALID PARAMETER ==
125INT: 1234
126EUC-JP:
127mb_convert_encoding(): Argument #2 ($to_encoding) must be a valid encoding, "BAD" given
128