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
80echo "== INVALID PARAMETER ==\n";
81
82$s = mb_convert_encoding(1234, 'EUC-JP');
83print("INT: $s\n");
84
85$s = mb_convert_encoding('', 'EUC-JP');
86print("EUC-JP: $s\n");  // SJIS
87
88function tryBadConversion($str, $encoding) {
89    try {
90        var_dump(mb_convert_encoding($str, $encoding));
91    } catch (ValueError $e) {
92        echo $e->getMessage(), "\n";
93    }
94}
95
96tryBadConversion($euc_jp, 'BAD');
97
98tryBadConversion('abc', 'Quoted-Printable');
99tryBadConversion('abc', 'BASE64');
100tryBadConversion('abc', 'HTML-ENTITIES');
101
102?>
103--EXPECTF--
104== BASIC TEST ==
105EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
106EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
107SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
108JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
109== STRING ENCODING LIST ==
110EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
111SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
112JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
113== ARRAY ENCODING LIST ==
114EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
115SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
116JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
117UTF-8: test
118== DETECT ORDER ==
119EUC-JP: c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
120SJIS: k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
121JIS: GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
122== INVALID PARAMETER ==
123INT: 1234
124EUC-JP:
125mb_convert_encoding(): Argument #2 ($to_encoding) must be a valid encoding, "BAD" given
126
127Deprecated: mb_convert_encoding(): Handling QPrint via mbstring is deprecated; use quoted_printable_encode/quoted_printable_decode instead in %s on line %d
128string(3) "abc"
129
130Deprecated: mb_convert_encoding(): Handling Base64 via mbstring is deprecated; use base64_encode/base64_decode instead in %s on line %d
131string(4) "YWJj"
132
133Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in %s on line %d
134string(3) "abc"
135