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