xref: /PHP-8.2/ext/mbstring/tests/bug54494.phpt (revision 39131219)
1--TEST--
2Bug #54494: mb_substr() mishandles UTF-32LE and UCS-2LE
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7
8//declare(encoding = 'UTF-8');
9mb_internal_encoding('UTF-8');
10
11header('Content-Type: text/plain; charset=UTF-32LE');
12
13$stringOr = "hällö wörld\n";
14
15$mode = "UTF-32LE";
16
17echo "$mode:\n";
18
19$string = mb_convert_encoding($stringOr, $mode);
20$length = mb_strlen($string, $mode);
21echo "Length: ", $length, "\n";
22
23
24for ($i=0; $i < $length; $i++) {
25  $t = unpack("H*",mb_substr($string, $i, 1, $mode));
26  echo $t[1];
27}
28echo "\n";
29
30
31$mode = "UCS-2LE";
32
33echo "$mode:\n";
34
35$string = mb_convert_encoding($stringOr, $mode);
36$length = mb_strlen($string, $mode);
37echo "Length: ", $length, "\n";
38
39
40for ($i=0; $i < $length; $i++) {
41  $t = unpack("H*",mb_substr($string, $i, 1, $mode));
42  echo $t[1];
43}
44echo "\n";
45?>
46--EXPECT--
47UTF-32LE:
48Length: 12
4968000000e40000006c0000006c000000f60000002000000077000000f6000000720000006c000000640000000a000000
50UCS-2LE:
51Length: 12
526800e4006c006c00f60020007700f60072006c0064000a00
53