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