File: mb_str_split_error_conditions.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (59 lines) | stat: -rw-r--r-- 1,906 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--TEST--
mb_str_split() error conditions
--EXTENSIONS--
mbstring
--FILE--
<?php

$string = "日本"; /* 2 chars */

// Invalid split length
try {
    mb_str_split($string, 0);
} catch (\ValueError $e) {
    echo $e->getMessage() . \PHP_EOL;
}
try {
    mb_str_split($string, -5);
} catch (\ValueError $e) {
    echo $e->getMessage() . \PHP_EOL;
}

//Invalid Encoding
try {
    mb_str_split($string, 1, "BAD_ENCODING");
} catch (\ValueError $e) {
    echo $e->getMessage() . \PHP_EOL;
}

// For UTF-8, error markers are not inserted
echo "== INVALID UTF-8 ==\n";
$array = mb_str_split("abc\xFFabc", 2, "UTF-8");
echo "[", implode(', ', array_map('bin2hex', $array)), "]\n";

// For most other encodings, they are
echo "== INVALID HZ ==\n";
// The last string emitted by mb_str_split will include '?' as an error marker,
// since ά cannot be represented in HZ
$array = mb_str_split(mb_convert_encoding("ελληνικά", "HZ", "UTF-8"), 2, "HZ");
echo "[", implode(', ', array_map('bin2hex', $array)), "]\n";

// HTML entity error markers
mb_substitute_character("entity");
echo "== INVALID HZ IN 'ENTITY' ERROR OUTPUT MODE ==\n";
// The output here will actually include an HTML entity #x3AC;
// It will be split into segments of 2 characters each by mb_str_split
$array = mb_str_split(mb_convert_encoding("ελληνικά", "HZ", "UTF-8"), 2, "HZ");
echo "[", implode(', ', array_map('bin2hex', $array)), "]\n";

?>
--EXPECT--
mb_str_split(): Argument #2 ($length) must be greater than 0
mb_str_split(): Argument #2 ($length) must be greater than 0
mb_str_split(): Argument #3 ($encoding) must be a valid encoding, "BAD_ENCODING" given
== INVALID UTF-8 ==
[6162, 63ff, 6162, 63]
== INVALID HZ ==
[7e7b2645264b7e7d, 7e7b264b26477e7d, 7e7b264d26497e7d, 7e7b264a7e7d3f]
== INVALID HZ IN 'ENTITY' ERROR OUTPUT MODE ==
[7e7b2645264b7e7d, 7e7b264b26477e7d, 7e7b264d26497e7d, 7e7b264a7e7d26, 2378, 3341, 433b]