1--TEST--
2mb_convert_encoding()
3--EXTENSIONS--
4mbstring
5--INI--
6output_handler=
7mbstring.language=Japanese
8--FILE--
9<?php
10// TODO: Add more tests
11
12$sjis = base64_decode('k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==');
13$jis = base64_decode('GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==');
14$euc_jp = "\xC6\xFC\xCB\xDC\xB8\xEC\xA5\xC6\xA5\xAD\xA5\xB9\xA5\xC8\xA4\xC7\xA4\xB9\xA1\xA301234\xA3\xB5\xA3\xB6\xA3\xB7\xA3\xB8\xA3\xB9\xA1\xA3";
15
16// Test with single "form encoding"
17echo "== BASIC TEST ==\n";
18$s = bin2hex(mb_convert_encoding($sjis, 'EUC-JP', 'SJIS'));
19print("EUC-JP: $s\n"); // EUC-JP
20
21$s = bin2hex(mb_convert_encoding($jis, 'EUC-JP', 'JIS'));
22print("EUC-JP: $s\n"); // EUC-JP
23
24$s = mb_convert_encoding($euc_jp, 'SJIS', 'EUC-JP');
25print("SJIS: ".base64_encode($s)."\n"); // SJIS
26
27$s = mb_convert_encoding($euc_jp, 'JIS', 'EUC-JP');
28print("JIS: ".base64_encode($s)."\n"); // JIS
29
30// Using Encoding List Array
31echo "== STRING ENCODING LIST ==\n";
32
33$a = 'JIS,UTF-8,EUC-JP,SJIS';
34$s = $jis;
35$s = bin2hex(mb_convert_encoding($s, 'EUC-JP', $a));
36print("EUC-JP: $s\n"); // EUC-JP
37
38$s = $euc_jp;
39$s = mb_convert_encoding($s, 'SJIS', $a);
40print("SJIS: ".base64_encode($s)."\n");  // SJIS
41
42$s = $euc_jp;
43$s = mb_convert_encoding($s, 'JIS', $a);
44print("JIS: ".base64_encode($s)."\n"); // JIS
45
46// Using Encoding List Array
47echo "== ARRAY ENCODING LIST ==\n";
48
49$a = ['JIS', 'UTF-8', 'EUC-JP', 'SJIS'];
50$s = $jis;
51$s = bin2hex(mb_convert_encoding($s, 'EUC-JP', $a));
52print("EUC-JP: $s\n"); // EUC-JP
53
54$s = $euc_jp;
55$s = mb_convert_encoding($s, 'SJIS', $a);
56print("SJIS: ".base64_encode($s)."\n");  // SJIS
57
58$s = $euc_jp;
59$s = mb_convert_encoding($s, 'JIS', $a);
60print("JIS: ".base64_encode($s)."\n"); // JIS
61
62// Regression test for bug #81676
63echo "UTF-8: " . mb_convert_encoding('test', 'UTF-8', mb_list_encodings()), "\n";
64
65// Using Detect Order
66echo "== DETECT ORDER ==\n";
67
68$s = $jis;
69$s = bin2hex(mb_convert_encoding($s, 'EUC-JP', 'auto'));
70print("EUC-JP: $s\n"); // EUC-JP
71
72$s = $euc_jp;
73$s = mb_convert_encoding($s, 'SJIS', 'auto');
74print("SJIS: ".base64_encode($s)."\n");  // SJIS
75
76$s = $euc_jp;
77$s = mb_convert_encoding($s, 'JIS', 'auto');
78print("JIS: ".base64_encode($s)."\n"); // JIS
79
80
81// Invalid Parameters
82echo "== INVALID PARAMETER ==\n";
83
84$s = mb_convert_encoding(1234, 'EUC-JP');
85print("INT: $s\n");
86
87$s = mb_convert_encoding('', 'EUC-JP');
88print("EUC-JP: $s\n");  // SJIS
89
90$s = $euc_jp;
91try {
92    var_dump(mb_convert_encoding($s, 'BAD'));
93} catch (\ValueError $e) {
94    echo $e->getMessage() . \PHP_EOL;
95}
96
97?>
98--EXPECT--
99== BASIC TEST ==
100EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
101EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
102SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
103JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
104== STRING ENCODING LIST ==
105EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
106SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
107JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
108== ARRAY ENCODING LIST ==
109EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
110SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
111JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
112UTF-8: test
113== DETECT ORDER ==
114EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
115SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
116JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
117== INVALID PARAMETER ==
118INT: 1234
119EUC-JP:
120mb_convert_encoding(): Argument #2 ($to_encoding) must be a valid encoding, "BAD" given
121