xref: /PHP-7.4/ext/standard/tests/strings/substr.phpt (revision d7a3edd4)
1--TEST--
2Testing substr() function
3--FILE--
4<?php
5
6/* Prototype: string substr( string str, int start[, int length] )
7 * Description: Returns the portion of string specified by the start and length parameters.
8 */
9
10$strings_array = array( NULL, "", 12345, "abcdef", "123abc", "_123abc");
11
12
13/* Testing for error conditions */
14echo "*** Testing for error conditions ***\n";
15
16/* Zero Argument */
17var_dump( substr() );
18
19/* NULL as Argument */
20var_dump( substr(NULL) );
21
22/* Single Argument */
23var_dump( substr("abcde") );
24
25/* Scalar Argument */
26var_dump( substr(12345) );
27
28/* more than valid number of arguments ( valid are 2 or 3 ) */
29var_dump( substr("abcde", 2, 3, 4) );
30
31$counter = 1;
32foreach ($strings_array as $str) {
33  /* variations with two arguments */
34  /* start values >, < and = 0    */
35
36  echo ("\n--- Iteration ".$counter." ---\n");
37  echo ("\n-- Variations for two arguments --\n");
38  var_dump ( substr($str, 1) );
39  var_dump ( substr($str, 0) );
40  var_dump ( substr($str, -2) );
41
42  /* variations with three arguments */
43  /* start value variations with length values  */
44
45  echo ("\n-- Variations for three arguments --\n");
46  var_dump ( substr($str, 1, 3) );
47  var_dump ( substr($str, 1, 0) );
48  var_dump ( substr($str, 1, -3) );
49  var_dump ( substr($str, 0, 3) );
50  var_dump ( substr($str, 0, 0) );
51  var_dump ( substr($str, 0, -3) );
52  var_dump ( substr($str, -2, 3) );
53  var_dump ( substr($str, -2, 0 ) );
54  var_dump ( substr($str, -2, -3) );
55
56  $counter++;
57}
58
59/* variation of start and length to point to same element */
60echo ("\n*** Testing for variations of start and length to point to same element ***\n");
61var_dump (substr("abcde" , 2, -2) );
62var_dump (substr("abcde" , -3, -2) );
63
64/* Testing to return empty string when start denotes the position beyond the truncation (set by negative length) */
65echo ("\n*** Testing for start > truncation  ***\n");
66var_dump (substr("abcdef" , 4, -4) );
67
68/* String with null character */
69echo ("\n*** Testing for string with null characters ***\n");
70var_dump (substr("abc\x0xy\x0z" ,2) );
71
72/* String with international characters */
73echo ("\n*** Testing for string with international characters ***\n");
74var_dump (substr('\xIñtërnâtiônàlizætiøn',3) );
75
76/* start <0 && -start > length */
77echo "\n*** Start before the first char ***\n";
78var_dump (substr("abcd" , -8) );
79
80echo"\nDone";
81
82?>
83--EXPECTF--
84*** Testing for error conditions ***
85
86Warning: substr() expects at least 2 parameters, 0 given in %s on line %d
87NULL
88
89Warning: substr() expects at least 2 parameters, 1 given in %s on line %d
90NULL
91
92Warning: substr() expects at least 2 parameters, 1 given in %s on line %d
93NULL
94
95Warning: substr() expects at least 2 parameters, 1 given in %s on line %d
96NULL
97
98Warning: substr() expects at most 3 parameters, 4 given in %s on line %d
99NULL
100
101--- Iteration 1 ---
102
103-- Variations for two arguments --
104bool(false)
105string(0) ""
106string(0) ""
107
108-- Variations for three arguments --
109bool(false)
110bool(false)
111bool(false)
112string(0) ""
113string(0) ""
114bool(false)
115string(0) ""
116string(0) ""
117bool(false)
118
119--- Iteration 2 ---
120
121-- Variations for two arguments --
122bool(false)
123string(0) ""
124string(0) ""
125
126-- Variations for three arguments --
127bool(false)
128bool(false)
129bool(false)
130string(0) ""
131string(0) ""
132bool(false)
133string(0) ""
134string(0) ""
135bool(false)
136
137--- Iteration 3 ---
138
139-- Variations for two arguments --
140string(4) "2345"
141string(5) "12345"
142string(2) "45"
143
144-- Variations for three arguments --
145string(3) "234"
146string(0) ""
147string(1) "2"
148string(3) "123"
149string(0) ""
150string(2) "12"
151string(2) "45"
152string(0) ""
153string(0) ""
154
155--- Iteration 4 ---
156
157-- Variations for two arguments --
158string(5) "bcdef"
159string(6) "abcdef"
160string(2) "ef"
161
162-- Variations for three arguments --
163string(3) "bcd"
164string(0) ""
165string(2) "bc"
166string(3) "abc"
167string(0) ""
168string(3) "abc"
169string(2) "ef"
170string(0) ""
171string(0) ""
172
173--- Iteration 5 ---
174
175-- Variations for two arguments --
176string(5) "23abc"
177string(6) "123abc"
178string(2) "bc"
179
180-- Variations for three arguments --
181string(3) "23a"
182string(0) ""
183string(2) "23"
184string(3) "123"
185string(0) ""
186string(3) "123"
187string(2) "bc"
188string(0) ""
189string(0) ""
190
191--- Iteration 6 ---
192
193-- Variations for two arguments --
194string(6) "123abc"
195string(7) "_123abc"
196string(2) "bc"
197
198-- Variations for three arguments --
199string(3) "123"
200string(0) ""
201string(3) "123"
202string(3) "_12"
203string(0) ""
204string(4) "_123"
205string(2) "bc"
206string(0) ""
207string(0) ""
208
209*** Testing for variations of start and length to point to same element ***
210string(1) "c"
211string(1) "c"
212
213*** Testing for start > truncation  ***
214bool(false)
215
216*** Testing for string with null characters ***
217string(6) "c�xy�z"
218
219*** Testing for string with international characters ***
220string(26) "ñtërnâtiônàlizætiøn"
221
222*** Start before the first char ***
223string(4) "abcd"
224
225Done
226