1--TEST--
2mb_detect_encoding()
3--EXTENSIONS--
4mbstring
5--INI--
6mbstring.language=Japanese
7--FILE--
8<?php
9// SJIS string (BASE64 encoded)
10$sjis = base64_decode('k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==');
11// JIS string (BASE64 encoded)
12$jis = base64_decode('GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==');
13// EUC-JP string
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// UTF-8
16$polish1 = "Zażółć gęślą jaźń.";
17$polish2 = "Wół poszedł spać bardzo wcześnie. A to zdanie bez ogonka.";
18$hungarian = "Árvíztűrő tükörfúrógép";
19
20echo "== BASIC TEST ==\n";
21
22print("Empty String: " . mb_detect_encoding('') . "\n");
23print("Bad ASCII (non-strict): " . mb_detect_encoding("\xDD\x92", ['ASCII', 'UTF-8'], false) . "\n");
24print("Bad ASCII (strict): " . mb_detect_encoding("\xDD\x92", ['ASCII', 'UTF-8'], true) . "\n");
25print("Bad ASCII/UTF-8, with more errors for ASCII (non-strict): " . mb_detect_encoding("\xD6\x8A\x8A", ['ASCII', 'UTF-8'], false) . "\n");
26print("Bad ASCII/UTF-8, with more errors for ASCII (strict): " . var_export(mb_detect_encoding("\xD6\x8A\x8A", ['ASCII', 'UTF-8'], true), true) . "\n");
27
28print("UTF-8 BOM (non-strict): " . mb_detect_encoding("\xEF\xBB\xBF", ["UTF-8", "ASCII"], false) . "\n");
29print("UTF-8 BOM (strict): " . mb_detect_encoding("\xEF\xBB\xBF", ["UTF-8", "ASCII"], true) . "\n");
30print("UTF-16BE BOM (non-strict): " . mb_detect_encoding("\xFE\xFF", ["UTF-8", "UTF-16BE", "UTF-16LE"], false) . "\n");
31print("UTF-16BE BOM (strict): " . mb_detect_encoding("\xFE\xFF", ["UTF-8", "UTF-16BE", "UTF-16LE"], true) . "\n");
32print("UTF-16LE BOM (non-strict): " . mb_detect_encoding("\xFF\xFE", ["UTF-8", "UTF-16BE", "UTF-16LE"], false) . "\n");
33print("UTF-16LE BOM (strict): " . mb_detect_encoding("\xFF\xFE", ["UTF-8", "UTF-16BE", "UTF-16LE"], true) . "\n");
34
35print("SJIS: " . mb_detect_encoding($sjis, 'SJIS', true) . "\n");
36print("JIS: " . mb_detect_encoding($jis, 'JIS', true) . "\n");
37print("EUC-JP (strict): " . mb_detect_encoding($euc_jp, 'UTF-8,EUC-JP,JIS', true) . "\n");
38print("EUC-JP (non-strict): " . mb_detect_encoding($euc_jp, 'UTF-8,EUC-JP,JIS', false) . "\n");
39print("EUC-JP (fewer choices): " . mb_detect_encoding($euc_jp, 'JIS,EUC-JP') . "\n");
40print("UTF-8, polish string 1 (strict): " . mb_detect_encoding($polish1, 'UTF-8,UTF-16,ISO-8859-1', true) . "\n");
41print("UTF-8, polish string 1 (non-strict): " . mb_detect_encoding($polish1, 'UTF-8,UTF-16,ISO-8859-1', false) . "\n");
42print("UTF-8, polish string 2 (strict): " . mb_detect_encoding($polish2, 'UTF-8,UTF-16,ISO-8859-1', true) . "\n");
43print("UTF-8, polish string 2 (non-strict): " . mb_detect_encoding($polish2, 'UTF-8,UTF-16,ISO-8859-1', false) . "\n");
44
45echo "== ARRAY ENCODING LIST ==\n";
46
47$a = ['UTF-8', 'EUC-JP', 'SJIS', 'JIS'];
48print("JIS: " . mb_detect_encoding($jis, $a) . "\n");
49print("EUC-JP: " . mb_detect_encoding($euc_jp, $a) . "\n");
50print("SJIS: " . mb_detect_encoding($sjis, $a) . "\n");
51
52$test = "CHARSET=windows-1252:Do\xeb;John";
53$encodings = [
54    'UTF-8', 'SJIS', 'GB2312',
55    'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
56    'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9',
57    'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
58    'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R', 'BIG-5',
59    'ISO-2022-KR', 'ISO-2022-JP', 'UTF-16'
60];
61echo mb_detect_encoding($test, $encodings), "\n";
62
63$test = 'N:Müller;Jörg;;;
64X-ABUID:2E4CB084-4767-4C85-BBCA-805B1DCB1C8E\:ABPerson';
65echo mb_detect_encoding($test, ['UTF-8', 'SJIS']), "\n";
66
67$test = 'BEGIN:VCARD
68VERSION:2.1
69N;ENCODING=QUOTED-PRINTABLE:Iksi=F1ski;Piotr
70FN;ENCODING=QUOTED-PRINTABLE:Piotr Iksi=F1ski
71EMAIL;PREF;INTERNET:piotr.iksinski@somedomain.com
72X-GENDER:Male
73REV:20080716T203548Z
74END:VCARD
75';
76echo mb_detect_encoding($test, ['UTF-8', 'UTF-16']), "\n";
77
78$test = 'Dušan';
79echo mb_detect_encoding($test, ['UTF-8', 'ISO-8859-1']), "\n"; // Should be UTF-8
80
81$test = 'Živko';
82echo mb_detect_encoding($test, ['UTF-8', 'ISO-8859-1']), "\n"; // Should be UTF-8
83
84// We once had a problem where all kind of strings would be detected as 'UUENCODE'
85echo mb_detect_encoding('abc', ['UUENCODE', 'UTF-8']), "\n";
86echo mb_detect_encoding('abc', ['UUENCODE', 'QPrint', 'HTML-ENTITIES', 'Base64', '7bit', '8bit', 'SJIS']), "\n";
87
88// This test case courtesy of Adrien Foulon
89// It depends on the below use of '+' being recognized as invalid UTF-7
90$css = 'input[type="radio"]:checked + img {
91    border: 5px solid #0083ca;
92}';
93echo mb_detect_encoding($css, mb_list_encodings(), true), "\n";
94
95// Test cases courtesy of Kirill Roskolii and Chris Burgess
96echo "-- Māori text --\n";
97
98echo mb_detect_encoding("Total Māori,31.5,33.3,31.8,33,36.4,33.2,33.2", ['UTF-8', 'ISO-8859-1', 'Windows-1251']), "\n";
99// Names of native birds from Aotearoa:
100echo mb_detect_encoding("Kākā", ['UTF-8', 'ISO-8859-1', 'Windows-1251']), "\n";
101echo mb_detect_encoding("Whēkau", ['UTF-8', 'ISO-8859-1', 'Windows-1251']), "\n";
102echo mb_detect_encoding("Tīwaiwaka", ['UTF-8', 'ISO-8859-1', 'Windows-1251']), "\n";
103echo mb_detect_encoding("Kōtuku", ['UTF-8', 'ISO-8859-1', 'Windows-1251']), "\n";
104echo mb_detect_encoding("Kererū", ['UTF-8', 'ISO-8859-1', 'Windows-1251']), "\n";
105echo mb_detect_encoding("Tūī", ['UTF-8', 'ISO-8859-1', 'Windows-1251']), "\n";
106
107echo "== DETECT ORDER ==\n";
108
109mb_detect_order('auto');
110
111print("JIS: " . mb_detect_encoding($jis) . "\n");
112
113print("EUC-JP: " . mb_detect_encoding($euc_jp) . "\n");
114
115print("SJIS: " . mb_detect_encoding($sjis) . "\n");
116
117// Thanks to Ulrik Nielsen for the following tests; the hex strings are the same file, but in two
118// different text encodings
119// We do not have any strong hints showing that the second one is actually UTF-8...
120// but mb_detect_encoding still guesses UTF-8 because it is the first one in the list
121
122$win1252text = hex2bin("2320546869732066696c6520636f6e7461696e732057696e646f77732d3132353220656e636f646564206461746120616e642048544d4c20656e7469746965730a61626364650ae6f8e50af00a3c703e476f646461673c6272202f3e0a7b726561646f626a206f626a65637469643d24726573756c745b305d2e706172656e7469642061737369676e3d22646f63227d3c6272202f3e0a23205468697320697320746f20656e73757265207468617420646966666572656e74206b696e6473206f662048544d4c20656e74697469657320617265206265696e6720636f6e76657274656420636f72726563746c790af00ad00a2623383739313b0a262331373238373b0a262333383937393b0a2623353437333b0a616263646520e6f8e520f020d0203c703e476f646461673c6272202f3e207b726561646f626a206f626a65637469643d24726573756c745b305d2e706172656e7469642061737369676e3d22646f63227d3c6272202f3e202623383739313b20262331373238373b20262333383937393b202623353437333b0a232054686520666f6c6c6f77696e67206368617261637465727320617265207370656369616c206368617261637465727320746861742068617320646966666572656e7420636f646520706f696e747320696e2049534f2d383835392d3120616e642057696e646f77732d31323532202d207468617420776520646966666572656e746961746520636f72726563746c79206265747765656e2049534f2d383835392d3120616e642057696e646f77732d313235320a8c0a890a2320506f6c69736820737472696e670a50727a656a6426233337383b20646f2070727a65676c26233236313b64750a");
123echo mb_detect_encoding($win1252text, ['UTF-8', 'CP1252', 'ISO-8859-1'], true), "\n";
124
125$utf8text = hex2bin("2320546869732066696c6520636f6e7461696e73205554462d3820656e636f64656420646174610a61626364650ac3a6c3b8c3a50ac3b00a3c703e476f646461673c6272202f3e0a7b726561646f626a206f626a65637469643d24726573756c745b305d2e706172656e7469642061737369676e3d22646f63227d3c6272202f3e0a23205468697320697320746f20656e73757265207468617420646966666572656e74206b696e6473206f662048544d4c20656e74697469657320617265206265696e6720636f6e76657274656420636f72726563746c790ac3b00ac3900ae289970ae48e870ae9a1830ae195a10a616263646520c3a6c3b8c3a520c3b020c390203c703e476f646461673c6272202f3e207b726561646f626a206f626a65637469643d24726573756c745b305d2e706172656e7469642061737369676e3d22646f63227d3c6272202f3e20e2899720e48e8720e9a18320e195a10a232054686520666f6c6c6f77696e67206368617261637465727320617265207370656369616c206368617261637465727320746861742068617320646966666572656e7420636f646520706f696e747320696e2049534f2d383835392d3120616e642057696e646f77732d31323532202d207468617420776520646966666572656e746961746520636f72726563746c79206265747765656e2049534f2d383835392d3120616e642057696e646f77732d313235320ac5920ae280b00a2320506f6c69736820737472696e670a50727a656a64c5ba20646f2070727a65676cc48564750a");
126echo mb_detect_encoding($utf8text, ['UTF-8', 'CP1252', 'ISO-8859-1'], true), "\n";
127
128echo "== INVALID PARAMETER ==\n";
129
130print("INT: " . mb_detect_encoding(1234, 'EUC-JP') . "\n"); // EUC-JP
131
132print("EUC-JP: " . mb_detect_encoding('', 'EUC-JP') . "\n");  // SJIS
133
134try {
135    var_dump(mb_detect_encoding($euc_jp, 'BAD'));
136} catch (\ValueError $e) {
137    echo $e->getMessage() . \PHP_EOL;
138}
139
140echo "== BOM TEST ==\n";
141
142$str = chr(239).chr(187).chr(191).chr(195).chr(180); // UTF-8 BOM followed by ô
143var_dump(mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1'], true));
144// U+4E4E is the Chinese character 乎; normally it would be impossible to distinguish UTF-16LE from UTF-16BE
145// But the BOM can tell us which one it is
146var_dump(mb_detect_encoding("\xFE\xFF\x4E\x4E", ['UTF-8', 'ISO-8859-1', 'UTF-16LE', 'UTF-16BE'], true));
147var_dump(mb_detect_encoding("\xFF\xFE\x4E\x4E", ['UTF-8', 'ISO-8859-1', 'UTF-16LE', 'UTF-16BE'], true));
148// However, a BOM should only appear at the beginning of the string
149$detected = mb_detect_encoding("\x4E\x4E\xFE\xFF\x4E\x4E", ['UTF-8', 'ISO-8859-1', 'UTF-16LE', 'UTF-16BE'], true);
150if ($detected === 'UTF-16BE' || $detected === 'UTF-16LE')
151    die("Don't accept a BOM in the middle of a string");
152
153echo "== CHECK FUNCTION TEST ==\n";
154
155function testCheckFn($str, $encoding, $encodings) {
156    if (mb_check_encoding($str, $encoding))
157        die("Input string " . bin2hex($str) . " should not be valid in " . $encoding);
158    if (mb_detect_encoding($str, $encodings, true) === $encoding)
159        die("mb_detect_encoding should never return " . $encoding . " for invalid input string " . bin2hex($str));
160}
161
162testCheckFn("abc + abc", "UTF-7", "UTF-7,UTF-8");
163
164echo "== TORTURE TEST ==\n";
165
166function test($strings, $encodings) {
167    foreach ($strings as $example) {
168        foreach ($encodings as $encoding) {
169            $converted = mb_convert_encoding($example, $encoding, 'UTF-8');
170            $detected = mb_detect_encoding($converted, $encodings);
171            if ($detected !== $encoding) {
172                echo "BAD! mb_detect_encoding returned $detected (should have been $encoding)\n";
173                echo "UTF-8 was: $example\n";
174                echo "$encoding bytes: ", bin2hex($converted), "\n";
175            }
176        }
177    }
178}
179
180$jpStrings = [
181    // Hat tip to Wikipedia
182    "日本で生まれ育ったほとんどの人は、日本語を母語とする[注 3]",
183    "2019年4月現在、インターネット上の言語使用者数は、英語、中国語、スペイン語、アラビア語、ポルトガル語、マレー語/インドネシア語、フランス語に次いで8番目に多い[13][信頼性要検証]。",
184    "日本語は地方ごとに多様な方言があり、とりわけ琉球諸島[要曖昧さ回避]で方言差が著しい(「方言」の節参照)。",
185    "さ  し   す   せ   そ   しゃ  しゅ  しょ  (清音)
186     た   ち   つ   て   と   ちゃ  ちゅ  ちょ  (清音)
187     な   に   ぬ   ね   の   にゃ  にゅ  にょ  ――",
188    "明治時代に入り、1889年から大槻文彦編の小型辞書『言海』が刊行された。これは、古典語・日常語を網羅し、五十音順に見出しを並べて、品詞・漢字表記・語釈を付した初の近代的な日本語辞書であった。『言海』は、後の辞書の模範的存在となり、後に増補版の『大言海』も刊行された。",
189    "奈良時代には『楊氏漢語抄』や『弁色立成(べんしきりゅうじょう)』という辞書が編纂された。それぞれ逸文として残るのみであるが、和訓を有する漢和辞書であったらしい。",
190    "複雑な文字体系を理由に、日本語を特殊とする議論もある。",
191    "一時的流行語。ある時代の若い世代が使う言葉。戦後の「アジャパー」、1970年代の「チカレタビー」など。コーホート語(同世代語)。",
192    "外国人による日本語研究も、中世末期から近世前期にかけて多く行われた。イエズス会では日本語とポルトガル語の辞書『日葡辞書』(1603年)が編纂され、また、同会のロドリゲスにより文法書『日本大文典』(1608年)および『日本小文典』(1620年)が表された。",
193    "一方、戦後になると各地の方言が失われつつあることが危惧されるようになった。NHK放送文化研究所は、(昭和20年代の時点で)各地の純粋な方言は80歳以上の老人の間でのみ使われているにすぎないとして、1953年から5年計画で全国の方言の録音を行った。",
194    "文体史 和漢混淆文の誕生",
195    "仮名遣いについては、早く小学校令施行規則(1900年)において、「にんぎやう(人形)」を「にんぎょー」とするなど、漢字音を発音通りにする、いわゆる「棒引き仮名遣い」が採用されたことがあった。",
196    "元来、日本に文字と呼べるものはなく、言葉を表記するためには中国渡来の漢字を用いた(いわゆる神代文字は後世の偽作とされている[167])。",
197    "第二次世界大戦が激しくなるにつれて、外来語を禁止または自粛する風潮も起こったが、戦後はアメリカ発の外来語が爆発的に多くなった。",
198    "そこから類推した結果、「文字を読む」に対して「文字が読むる(読める)」などの可能動詞が出来上がったものと考えられる。",
199    "近代以降には、外国語(特に英語)の音の影響で新しい音が使われ始めた。比較的一般化した「シェ・チェ・ツァ・ツェ・ツォ・ティ・ファ・フィ・フェ・フォ・ジェ・ディ・デュ」などの音に加え、場合によっては、「イェ・ウィ・ウェ・ウォ・クァ・クィ・クェ・クォ・ツィ・トゥ・グァ・ドゥ・テュ・フュ」などの音も使われる[147]。",
200    "20世紀後半から21世紀初頭にかけて中央競馬のトップジョッキーとして活躍し、競馬ファンから名手の愛称で親しまれた。",
201    "名鉄モ600形電車(めいてつモ600がたでんしゃ)は、名古屋鉄道(名鉄)が岐阜地区の直流600 V電化路線区の一つである美濃町線において運用する目的で、1970年(昭和45年)に導入した電車である。",
202    "その視点から、真理は当初未就学期の娘を幼稚園に入園させる考えは持っていなかったが、",
203    // And here's to everyone's favorite blue robot...
204    "機械だって 涙を流して 震えながら 勇気を叫ぶだろう",
205    "台風だって 心を痛めて 愛を込めて さよならするだろう",
206    "便利な道具で 助けてくれる おもちゃの 兵器だ 「ソレ!とつげき!」"
207];
208$jpEncodings = [
209    'UTF-32BE',
210    'UTF-32LE',
211    'UTF-16BE',
212    'UTF-16LE',
213    'UTF-8',
214    'UTF-7',
215    'EUC-JP',
216    'SJIS',
217    'ISO-2022-JP'
218];
219test($jpStrings, $jpEncodings);
220
221$cnStrings = [
222    // Headline randomly picked from Chinese news
223    "日本宫内厅宣布,真子公主和小室圭将在10月26日完婚。",
224    // The Dream of Red Mansions
225    "此开卷第一回也。作者自云曾历过一番梦幻之后,故将真事隐去,而借“通灵”说此《石头记》一书也",
226    "一日,炎夏永昼,士隐于书房闲坐,手倦抛书,伏几盹睡。",
227    "  须臾,茶毕,早已设下杯盘。",
228    "士隐听了,便迎上来道:“你满口说些什么?只听见些“好了”“好了”。",
229    "士隐送雨村去后,回房一觉,直至红日三竿方醒。",
230    "时逢三五便团圞,满把清光护玉栏。",
231    "但弟子愚拙,不能洞悉明白。",
232    "按那石头上书云:当日地陷东南,这东南有个姑苏城,城中阊门",
233    "后来既受天地精华,复得甘露滋养,遂脱了草木之胎,幻化人形,仅仅修成女体,终日游于“离恨天”外,饥餐“秘情果”,渴饮“灌愁水”。",
234    "原来雨村自那日见了甄家丫鬟,曾回顾他两次,自谓是个知己,便时刻放在心上。",
235    // Wikipedia
236    // (A lot of this uses traditional Chinese characters, which we also want to be tested)
237    "漢语主要使用漢字書寫,為語素文字。",
238    "現漢字擁有兩套文字系統,分別為正體字與簡體字。",
239    "標準漢語中四個主要的聲調,使用ma這個音節發音。",
240    "在語言學原則上,互相之間不能通話的應該被定性為語言而非方言。",
241    "但不少詞彙會採用粵語詞彙(例如採用「巴士」而非「公車」,採用「魚蛋」而非「魚丸子」,採用「沙律」而非「色拉」)",
242    "這是因為其他國家(除日本外)均使用表音文字,對於“文”[6]與“語”[7]並不作區分,不符合漢語語法",
243    "主条目:闽语、閩東語、福州語、閩南語和臺灣閩南語",
244    "普通話中,ai,ei,ao,ou等都是雙元音韻母",
245    "汉字",
246    "实词,词汇中含有实际意义的词语",
247    "我的老師 一位顧客 恭敬地鞠躬 完全相信 非常堅強 多麼可愛",
248    "敬畏生命 熱愛工作 上中學 登泰山 蓋房子 包餃子",
249    "参见:外來語 § 漢語外來語、中文外來語、汉字文化圈和汉字复活",
250    "如果将汉语延深入汉文,则汉文的信息密度更大。",
251    "我們家蓋了新房子。",
252    "他是一個高而瘦的老人。",
253    "我們家的臺階低。",
254    "我們家蓋了新房子。",
255    "敵人監視着葦塘。",
256    "連詞:用來連接詞、短語或句子,表示前後有並列、遞進、轉折、因果、假設等關係。",
257    "「大去之期不遠矣」",
258    "“官话方言”绝大多数次级方言都没有入声",
259    "其中,闽南语不仅有 −p,−t,−k,也有模糊入声"
260];
261$cnEncodings = [
262    'UTF-32BE',
263    'UTF-32LE',
264    'UTF-16BE',
265    'UTF-16LE',
266    'UTF-8',
267    'UTF-7',
268    'GB18030',
269    'BIG-5'
270];
271test($cnStrings, $cnEncodings);
272
273$deStrings = [
274    // Much love to Wikipedia
275    "Die beiden Brücken über den Strelasund (2011)",
276    "die Rügenbrücke und der Rügendamm sowie die regelmäßig betriebenen Fährverbindungen zwischen Stralsund",
277    "Der „Rügendamm“ ist die erste feste Strelasundquerung",
278    "Koordinaten    ♁54° 18′ 39″ N, 13° 7′ 0″ O",
279    "Die ausschließlich dem Kraftfahrzeugverkehr",
280    "Die Brücke ermöglicht dem Schiffsverkehr eine Durchfahrtshöhe von 40 m.[1]",
281    "Nach der Hauptbrücke folgt die Vorlandbrücke Dänholm (BW 3), eine 532,3 m",
282    "Die alte, als Klappbrücke ausgeführte Ziegelgrabenbrücke ist 133 Meter lang",
283    "Vor allem das Fährdorf Stralow („stral“ bedeutet im Mittelniederdeutschen und im Slawischen „Pfeil“) entwickelte sich rasch.",
284    "1946 kam es aufgrund der Zerstörung der Brücken",
285    "Auf der Trajektstrecke verkehrten im ersten Jahr bereits 90.000 Fahrgäste",
286    "Mai 1897 zwei Schnellzugpaare zwischen Berlin und Saßnitz.",
287    "Der Damm im Ziegelgraben und zwischen dem Dänholm und dem Widerlager der Brücke wurde mit den bei den Eisenbahnarbeiten gewonnenen Böden verfüllt.",
288    "Dabei passierten die vier anderen Trajekte die „Altefähr“",
289    "Ebenfalls in den 1980er Jahren traten zunehmend Ermüdungserscheinungen an den stark beanspruchten Stahlüberbauten auf",
290    "Erste Planungen für einen neuen Rügendamm
291Die Kapazität der Eisenbahnbrücke war begrenzt:",
292    "bestehend aus den Firmen Walter Bau AG vereinigt mit Dywidag (später/nach v.g. Insolvenz durch die Dywidag Bau GmbH)",
293    "Bereits im Herbst 1998 erfolgten die ersten Bohrungen zur Untersuchung der Tragfähigkeit des Baugrundes im Bereich des Ziegelgrabens"
294];
295$deEncodings = [
296    'UTF-32BE',
297    'UTF-32LE',
298    'UTF-16BE',
299    'UTF-16LE',
300    'UTF-8',
301    'ISO-8859-1'
302    // TODO: It would be good if ISO-8859-2 and ISO-8859-15 can be accurately detected as well
303];
304test($deStrings, $deEncodings);
305
306test([$polish1, $polish2], ['UTF-32BE', 'UTF-32LE', 'UTF-16BE', 'UTF-16LE', 'UTF-8', 'ISO-8859-2']);
307
308$czechStrings = [
309    // Gotta love these Czech proverbs
310    // "Some like girls, others like muffins." Truer words were never spoken.
311    'Bezdomovec je doma všude.',
312    'Bez práce nejsou koláče.',
313    'Bez peněz do hospody nelez.',
314    'Bližší košile nežli kabát.',
315    'Boží mlýny melou pomalu, ale jistě.',
316    'Co je dovoleno pánovi, není dovoleno kmánovi.',
317    'Co je šeptem, to je s čertem.',
318    'Co je v domě, není pro mě.',
319    'Co je v domě, to se počítá.',
320    'Co jsi z úst vypustil, ani párem koní nedostaneš zpět.',
321    'Co můžeš udělat dnes, neodkládej na zítřek.',
322    'Co nejde po dobrým, to půjde po zlým.',
323    'Co oči nevidí, to srdce nebolí (a ruce neukradnou).',
324    'Co se škádlívá, to se rádo mívá.',
325    'Co se v mládí naučíš, ke stáru jako když najdeš.',
326    'Co sis uvařil, to si sněz.',
327    'Co tě nezabije, to tě posílí.',
328    'Cvik dělá mistra.',
329    'Co tě nepálí, nehas.',
330    'Co na srdci, to na jazyku.',
331    'Co nejde silou, jde rozumem.',
332    'Čas všechny rány zahojí.',
333    'Častá krůpěj kámen proráží.',
334    'Čím výše vystoupíš, tím hlouběji padáš.',
335    'Čím výše vystoupíš, tím větší rozhled.',
336    'Čiň čertu dobře, peklem se ti odmění.',
337    'Čistota – půl zdraví.',
338    'Dal ses na vojnu, tak bojuj.',
339    'Darovanému koni na zuby nehleď.',
340    'Devatero řemesel – desátá bída.',
341    'Dějiny píší vítězové.',
342    'Dobré slovo i železná vrata otvírá.',
343    'Dočkej času jako husa klasu.',
344    'Drzé čelo lepší než poplužní dvůr.',
345    'Dvakrát měř, jednou řež.',
346    'Důvěřuj, ale prověřuj',
347    'Hlad je nejlepší kuchař.',
348    'Hlad má velké oči.',
349    'Hloupý, kdo dává, hloupější, kdo nebere.',
350    'Hněv je špatný rádce.',
351    'Někdo rád holky, jinej zas vdolky.',
352    'Volům kroky a jelenům skoky.',
353    'Vrána k vráně sedá, rovný rovného si hledá.',
354    'Všeho nechám, už tam spěchám.',
355    'Všechna sláva, polní tráva.',
356    'Všeho s mírou.',
357    'Všechno zlé je pro něco dobré.',
358    'Všude dobře, doma nejlépe.',
359    'Vrána vráně oči nevyklove.',
360    'Výjimka potvrzuje pravidlo.',
361    'Vzduch – boží duch.',
362    'Za dobrotu na žebrotu.',
363    'Zadarmo ani kuře nehrabe.',
364    'Zahálky jsa služebníkem, neběduj, žes hadrníkem.',
365    'Z cizího krev neteče.',
366    'Zítra je taky den.',
367    'Zakázané ovoce chutná nejlépe.',
368    'Zlaté slovo, které zůstane v ústech.',
369    'Zvyk je železná košile.',
370    'Žába močál vždy najde.',
371    'Žádná píseň není tak dlouhá, aby jí nebyl konec.',
372    'Žádný strom neroste do nebe.',
373    'Žádný učený z nebe nespadl.',
374    'Žízeň je věčná.'
375];
376$czechEncodings = [
377    'UTF-8',
378    'UTF-16',
379    // 'Windows-1250', // Windows-1250 is not supported by mbstring
380    // 'ISO-8859-2' // We are not able to accurately distinguish UTF-8 and ISO-8859-2
381];
382test($czechStrings, $czechEncodings);
383
384test([$hungarian], ['UTF-8', 'UTF-16', 'Windows-1252']);
385
386$turkishStrings = [
387    // Random junk indiscriminately copied from randomly picked Wikipedia articles
388    "Samsun 19 Mayıs Stadyumu, Samsun'un Tekkeköy ilçesinde bulunan akıllı çok amaçlı stadyumdur. 33.919 koltuk kapasitesiyle Samsunspor'un iç saha maçlarına ev sahipliği yapmaktadır. Toplu Konut İdaresi tarafından yaptırılan ve 2017'de tamamlanan stadyum adını Mustafa Kemal'in Samsun'a çıktığı gün olan 19 Mayıs'tan almaktadır. İlk olarak Nisan 2011'de duyurulan proje 3 Aralık 2012'de Toplu Konut İdaresince yapılan ve Ali Acar İnşaat'ın kazandığı ihale ile resmiyete dökülmüş, 4 Ağustos 2013 tarihindeki temel atma töreni ile de inşa aşamasına geçilmiştir. İnşaatın başlangıç tarihinden itibaren en geç 800 gün içerisinde tamamlanması taahhüt edilse de UEFA'nın standartlarına uyum sağlamak için projede yenileme yapılması, çatının inşasıyla sorumlu şirketin iflas etmesi gibi sebeplerle tamamlanma süresi birkaç kez sarkmıştır. Bu bağlamda, ilk etapta 2014-15 sezonunda hazır hâle geleceği açıklanan stadyumun açılışı önce 2015-16, daha sonra 2016-17 sezonu başına ertelense de bu hedefler de yakalanamamıştır.",
389    "Lütf-i Celil (Osmanlı Türkçesi: ﻟﻄﻒ ﺟﻠﻴﻞ Anlamı: \"İlahi Lütuf\"), Osmanlı Donanması'nın Lütf-i Celil sınıfının öncü gemisi olan zırhlı savaş gemisidir. Başlangıçta Osmanlı İmparatorluğu'na bağlı özerk bir devlet olan Mısır Hidivliği tarafından sipariş edilen Lütf-i Celil, Osmanlı hükûmetinin Mısır'ı gemiyi teslim etmeye zorlaması ile Fransa'daki Forges et Chantiers de la Gironde tersanesinde yapım aşamasındayken Osmanlılara devredildi. Lütf-i Celil, 1877'de 93 Harbi sırasında aktif görevde bulundu ve Rus güçlerinin Tuna'yı geçmesini önlemek için operasyonlarda bulundu. 11 Mayıs'ta devriye gezerken bir Rus topçu bataryasıyla çatışmaya girdi.",
390    "Çoğu tarihçinin kanısına göre, ABD'nin üçüncü başkanı Jefferson, karısının ölümünün ardından kölesi Sally Hemings ile 38 yıl süren ilişkisi sırasında onun altı çocuğunun babası olmuştur.",
391    "2011 İran drama filmi Bir Ayrılık, 61. Berlin Film Festivali'nde Altın Ayı kazanarak, bu ödülü alan ilk İran filmi oldu.",
392    "Josef Bringas, isyan eden general Nikiforos Fokas'a karşı Konstantinopolis'e birlikler getirdi; asilerin Boğaziçi'ni geçmelerini engellemek için tüm gemileri tuttu; Nikiforos'un babası Bardas'ı rehin aldı ama başarılı olamadı."
393];
394// ISO-8859-9 and Windows-1254 are very similar and we can't really distinguish them from each other
395// But both of them should be distinguishable from UTF-8
396test($turkishStrings, ['UTF-8', 'UTF-16', 'ISO-8859-9']);
397test($turkishStrings, ['UTF-8', 'Windows-1254']);
398
399echo "Done!\n";
400
401?>
402--EXPECT--
403== BASIC TEST ==
404Empty String: ASCII
405Bad ASCII (non-strict): UTF-8
406Bad ASCII (strict): UTF-8
407Bad ASCII/UTF-8, with more errors for ASCII (non-strict): UTF-8
408Bad ASCII/UTF-8, with more errors for ASCII (strict): false
409UTF-8 BOM (non-strict): UTF-8
410UTF-8 BOM (strict): UTF-8
411UTF-16BE BOM (non-strict): UTF-16BE
412UTF-16BE BOM (strict): UTF-16BE
413UTF-16LE BOM (non-strict): UTF-16LE
414UTF-16LE BOM (strict): UTF-16LE
415SJIS: SJIS
416JIS: JIS
417EUC-JP (strict): EUC-JP
418EUC-JP (non-strict): EUC-JP
419EUC-JP (fewer choices): EUC-JP
420UTF-8, polish string 1 (strict): UTF-8
421UTF-8, polish string 1 (non-strict): UTF-8
422UTF-8, polish string 2 (strict): UTF-8
423UTF-8, polish string 2 (non-strict): UTF-8
424== ARRAY ENCODING LIST ==
425JIS: JIS
426EUC-JP: EUC-JP
427SJIS: SJIS
428ISO-8859-1
429UTF-8
430UTF-8
431UTF-8
432UTF-8
433UTF-8
434SJIS
435UTF-8
436-- Māori text --
437UTF-8
438UTF-8
439UTF-8
440UTF-8
441UTF-8
442UTF-8
443UTF-8
444== DETECT ORDER ==
445JIS: JIS
446EUC-JP: EUC-JP
447SJIS: SJIS
448Windows-1252
449UTF-8
450== INVALID PARAMETER ==
451INT: EUC-JP
452EUC-JP: EUC-JP
453mb_detect_encoding(): Argument #2 ($encodings) contains invalid encoding "BAD"
454== BOM TEST ==
455string(5) "UTF-8"
456string(8) "UTF-16BE"
457string(8) "UTF-16LE"
458== CHECK FUNCTION TEST ==
459== TORTURE TEST ==
460Done!
461