1--TEST--
2mb_str_split() error conditions
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7
8$string = "日本"; /* 2 chars */
9
10// Invalid split length
11try {
12    mb_str_split($string, 0);
13} catch (\ValueError $e) {
14    echo $e->getMessage() . \PHP_EOL;
15}
16try {
17    mb_str_split($string, -5);
18} catch (\ValueError $e) {
19    echo $e->getMessage() . \PHP_EOL;
20}
21
22//Invalid Encoding
23try {
24    mb_str_split($string, 1, "BAD_ENCODING");
25} catch (\ValueError $e) {
26    echo $e->getMessage() . \PHP_EOL;
27}
28
29// For UTF-8, error markers are not inserted
30echo "== INVALID UTF-8 ==\n";
31$array = mb_str_split("abc\xFFabc", 2, "UTF-8");
32echo "[", implode(', ', array_map('bin2hex', $array)), "]\n";
33
34// For most other encodings, they are
35echo "== INVALID HZ ==\n";
36// The last string emitted by mb_str_split will include '?' as an error marker,
37// since ά cannot be represented in HZ
38$array = mb_str_split(mb_convert_encoding("ελληνικά", "HZ", "UTF-8"), 2, "HZ");
39echo "[", implode(', ', array_map('bin2hex', $array)), "]\n";
40
41// HTML entity error markers
42mb_substitute_character("entity");
43echo "== INVALID HZ IN 'ENTITY' ERROR OUTPUT MODE ==\n";
44// The output here will actually include an HTML entity #x3AC;
45// It will be split into segments of 2 characters each by mb_str_split
46$array = mb_str_split(mb_convert_encoding("ελληνικά", "HZ", "UTF-8"), 2, "HZ");
47echo "[", implode(', ', array_map('bin2hex', $array)), "]\n";
48
49?>
50--EXPECT--
51mb_str_split(): Argument #2 ($length) must be greater than 0
52mb_str_split(): Argument #2 ($length) must be greater than 0
53mb_str_split(): Argument #3 ($encoding) must be a valid encoding, "BAD_ENCODING" given
54== INVALID UTF-8 ==
55[6162, 63ff, 6162, 63]
56== INVALID HZ ==
57[7e7b2645264b7e7d, 7e7b264b26477e7d, 7e7b264d26497e7d, 7e7b264a7e7d3f]
58== INVALID HZ IN 'ENTITY' ERROR OUTPUT MODE ==
59[7e7b2645264b7e7d, 7e7b264b26477e7d, 7e7b264d26497e7d, 7e7b264a7e7d26, 2378, 3341, 433b]
60