xref: /PHP-7.1/ext/mbstring/tests/bug54494.phpt (revision 113213f0)
1--TEST--
2Bug #54494: mb_substr() mishandles UTF-32LE and UCS-2LE
3--SKIPIF--
4<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
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--EXPECT--
46UTF-32LE:
47Length: 12
4868000000e40000006c0000006c000000f60000002000000077000000f6000000720000006c000000640000000a000000
49UCS-2LE:
50Length: 12
516800e4006c006c00f60020007700f60072006c0064000a00
52