1--TEST--
2Test mb_substr() function : usage variations - pass different integers to $length arg
3--SKIPIF--
4<?php
5extension_loaded('mbstring') or die('skip');
6function_exists('mb_substr') or die("skip mb_substr() is not available in this build");
7?>
8--FILE--
9<?php
10/*
11 * Test how mb_substr() behaves when passed a range of integers as $length argument
12 */
13
14echo "*** Testing mb_substr() : usage variations ***\n";
15
16mb_internal_encoding('UTF-8');
17
18$string_ascii = '+Is an English string'; //21 chars
19
20//Japanese string, 21 characters
21$string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=');
22
23/*
24 * Loop through integers as multiples of ten for $offset argument
25 * 60 is larger than *BYTE* count for $string_mb
26 */
27for ($i = -60; $i <= 60; $i += 10) {
28    if (@$a || @$b) {
29        $a = null;
30        $b = null;
31    }
32    echo "\n**-- Offset is: $i --**\n";
33    echo "-- ASCII String --\n";
34    $a = mb_substr($string_ascii, 1, $i);
35    var_dump(base64_encode($a));
36    echo "--Multibyte String --\n";
37    $b = mb_substr($string_mb, 1, $i, 'UTF-8');
38    if (strlen($a) == mb_strlen($b, 'UTF-8')) { // should return same length
39        var_dump(base64_encode($b));
40    } else {
41        echo "Difference in length of ASCII string and multibyte string\n";
42    }
43
44}
45
46echo "Done";
47?>
48--EXPECT--
49*** Testing mb_substr() : usage variations ***
50
51**-- Offset is: -60 --**
52-- ASCII String --
53string(0) ""
54--Multibyte String --
55string(0) ""
56
57**-- Offset is: -50 --**
58-- ASCII String --
59string(0) ""
60--Multibyte String --
61string(0) ""
62
63**-- Offset is: -40 --**
64-- ASCII String --
65string(0) ""
66--Multibyte String --
67string(0) ""
68
69**-- Offset is: -30 --**
70-- ASCII String --
71string(0) ""
72--Multibyte String --
73string(0) ""
74
75**-- Offset is: -20 --**
76-- ASCII String --
77string(0) ""
78--Multibyte String --
79string(0) ""
80
81**-- Offset is: -10 --**
82-- ASCII String --
83string(16) "SXMgYW4gRW5nbA=="
84--Multibyte String --
85string(40) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMA=="
86
87**-- Offset is: 0 --**
88-- ASCII String --
89string(0) ""
90--Multibyte String --
91string(0) ""
92
93**-- Offset is: 10 --**
94-- ASCII String --
95string(16) "SXMgYW4gRW5nbA=="
96--Multibyte String --
97string(40) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMA=="
98
99**-- Offset is: 20 --**
100-- ASCII String --
101string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc="
102--Multibyte String --
103string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
104
105**-- Offset is: 30 --**
106-- ASCII String --
107string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc="
108--Multibyte String --
109string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
110
111**-- Offset is: 40 --**
112-- ASCII String --
113string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc="
114--Multibyte String --
115string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
116
117**-- Offset is: 50 --**
118-- ASCII String --
119string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc="
120--Multibyte String --
121string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
122
123**-- Offset is: 60 --**
124-- ASCII String --
125string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc="
126--Multibyte String --
127string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
128Done
129