xref: /PHP-8.0/ext/standard/tests/strings/strstr.phpt (revision b5c7a83d)
1--TEST--
2Test strstr() function
3--FILE--
4<?php
5echo "*** Testing basic functionality of strstr() ***\n";
6var_dump( strstr("test string", "test") );
7var_dump( strstr("test string", "string") );
8var_dump( strstr("test string", "strin") );
9var_dump( strstr("test string", "t s") );
10var_dump( strstr("test string", "g") );
11var_dump( md5(strstr("te".chr(0)."st", chr(0))) );
12var_dump( strstr("tEst", "test") );
13var_dump( strstr("teSt", "test") );
14var_dump( strstr("", "") );
15var_dump( strstr("a", "") );
16var_dump( strstr("", "a") );
17
18
19echo "\n*** Testing strstr() with various needles ***";
20$string =
21"Hello world,012033 -3.3445     NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
22abcd$:Hello world";
23
24/* needles in an array to get the string starts with needle, in $string */
25$needles = array(
26  "Hello world",
27  "WORLD",
28  "\0",
29  "\x00",
30  "\x000",
31  "abcd",
32  "xyz",
33  "octal",
34  "-3",
35  -3,
36  "-3.344",
37  -3.344,
38  NULL,
39  "NULL",
40  "0",
41  0,
42  TRUE,
43  "TRUE",
44  "1",
45  1,
46  FALSE,
47  "FALSE",
48  " ",
49  "     ",
50  'b',
51  '\n',
52  "\n",
53  "12",
54  "12twelve",
55  $string
56);
57
58/* loop through to get the string starts with "needle" in $string */
59for( $i = 0; $i < count($needles); $i++ ) {
60  echo "\n-- Iteration $i --\n";
61  var_dump( strstr($string, $needles[$i]) );
62}
63
64
65echo "\n*** Testing miscellaneous input data ***\n";
66
67echo "-- Passing objects as string and needle --\n";
68/* we get "Recoverable fatal error: saying Object of class needle could not be
69converted to string" by default when an object is passed instead of string:
70The error can be  avoided by choosing the __toString magix method as follows: */
71
72class StringCapable
73{
74  function __toString() {
75    return "Hello, world";
76  }
77}
78$obj_string = new StringCapable;
79
80class needle
81{
82  function __toString() {
83    return "world";
84  }
85}
86$obj_needle = new needle;
87
88var_dump(strstr("$obj_string", "$obj_needle"));
89
90
91echo "\n-- Posiibilities with null --\n";
92var_dump( strstr("", NULL) );
93var_dump( strstr(NULL, NULL) );
94var_dump( strstr("a", NULL) );
95var_dump( strstr("/x0", "0") );  // Hexadecimal NUL
96
97echo "\n-- A longer and heredoc string --\n";
98$string = <<<EOD
99abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
100abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
101abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
102abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
103abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
104abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
105abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
106abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
107abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
108abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
109EOD;
110var_dump( strstr($string, "abcd") );
111var_dump( strstr($string, "1234") );
112
113echo "\n-- A heredoc null string --\n";
114$str = <<<EOD
115EOD;
116var_dump( strstr($str, "\0") );
117var_dump( strstr($str, NULL) );
118var_dump( strstr($str, "0") );
119
120
121echo "\n-- simple and complex syntax strings --\n";
122$needle = 'world';
123
124/* Simple syntax */
125var_dump( strstr("Hello, world", "$needle") );  // works
126var_dump( strstr("Hello, world'S", "$needle'S") );  // works
127var_dump( strstr("Hello, worldS", "$needleS") );  // won't work
128
129/* String with curly braces, complex syntax */
130var_dump( strstr("Hello, worldS", "${needle}S") );  // works
131var_dump( strstr("Hello, worldS", "{$needle}S") );  // works
132
133echo "\n*** Testing error conditions ***";
134var_dump( strstr($string, ""));
135var_dump( strstr("a", "b", "c") );  // args > expected
136var_dump( strstr(NULL, "") );
137
138?>
139
140DONE
141--EXPECTF--
142*** Testing basic functionality of strstr() ***
143string(11) "test string"
144string(6) "string"
145string(6) "string"
146string(8) "t string"
147string(1) "g"
148string(32) "7272696018bdeb2c9a3f8d01fc2a9273"
149bool(false)
150bool(false)
151string(0) ""
152string(1) "a"
153bool(false)
154
155*** Testing strstr() with various needles ***
156-- Iteration 0 --
157string(85) "Hello world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
158
159abcd$:Hello world"
160
161-- Iteration 1 --
162bool(false)
163
164-- Iteration 2 --
165string(39) "� abcd\xxyz �0 octal
166
167abcd$:Hello world"
168
169-- Iteration 3 --
170string(39) "� abcd\xxyz �0 octal
171
172abcd$:Hello world"
173
174-- Iteration 4 --
175string(27) "�0 octal
176
177abcd$:Hello world"
178
179-- Iteration 5 --
180string(37) "abcd\xxyz �0 octal
181
182abcd$:Hello world"
183
184-- Iteration 6 --
185string(31) "xyz �0 octal
186
187abcd$:Hello world"
188
189-- Iteration 7 --
190string(24) "octal
191
192abcd$:Hello world"
193
194-- Iteration 8 --
195string(66) "-3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
196
197abcd$:Hello world"
198
199-- Iteration 9 --
200string(66) "-3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
201
202abcd$:Hello world"
203
204-- Iteration 10 --
205string(66) "-3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
206
207abcd$:Hello world"
208
209-- Iteration 11 --
210string(66) "-3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
211
212abcd$:Hello world"
213
214-- Iteration 12 --
215string(85) "Hello world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
216
217abcd$:Hello world"
218
219-- Iteration 13 --
220string(54) "NULL TRUE FALSE� abcd\xxyz �0 octal
221
222abcd$:Hello world"
223
224-- Iteration 14 --
225string(73) "012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
226
227abcd$:Hello world"
228
229-- Iteration 15 --
230string(73) "012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
231
232abcd$:Hello world"
233
234-- Iteration 16 --
235string(72) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
236
237abcd$:Hello world"
238
239-- Iteration 17 --
240string(49) "TRUE FALSE� abcd\xxyz �0 octal
241
242abcd$:Hello world"
243
244-- Iteration 18 --
245string(72) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
246
247abcd$:Hello world"
248
249-- Iteration 19 --
250string(72) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
251
252abcd$:Hello world"
253
254-- Iteration 20 --
255string(85) "Hello world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
256
257abcd$:Hello world"
258
259-- Iteration 21 --
260string(44) "FALSE� abcd\xxyz �0 octal
261
262abcd$:Hello world"
263
264-- Iteration 22 --
265string(80) " world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
266
267abcd$:Hello world"
268
269-- Iteration 23 --
270string(59) "     NULL TRUE FALSE� abcd\xxyz �0 octal
271
272abcd$:Hello world"
273
274-- Iteration 24 --
275string(36) "bcd\xxyz �0 octal
276
277abcd$:Hello world"
278
279-- Iteration 25 --
280bool(false)
281
282-- Iteration 26 --
283string(19) "
284
285abcd$:Hello world"
286
287-- Iteration 27 --
288string(72) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
289
290abcd$:Hello world"
291
292-- Iteration 28 --
293bool(false)
294
295-- Iteration 29 --
296string(85) "Hello world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
297
298abcd$:Hello world"
299
300*** Testing miscellaneous input data ***
301-- Passing objects as string and needle --
302string(5) "world"
303
304-- Posiibilities with null --
305string(0) ""
306string(0) ""
307string(1) "a"
308string(1) "0"
309
310-- A longer and heredoc string --
311string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
312abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
313abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
314abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
315abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
316abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
317abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
318abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
319abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
320abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
321string(702) "123456789abcdefghijklmnopqrstuvwxyz0123456789
322abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
323abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
324abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
325abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
326abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
327abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
328abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
329abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
330abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
331
332-- A heredoc null string --
333bool(false)
334string(0) ""
335bool(false)
336
337-- simple and complex syntax strings --
338string(5) "world"
339string(7) "world'S"
340
341Warning: Undefined variable $needleS in %s on line %d
342string(13) "Hello, worldS"
343string(6) "worldS"
344string(6) "worldS"
345
346*** Testing error conditions ***string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
347abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
348abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
349abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
350abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
351abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
352abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
353abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
354abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
355abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
356bool(false)
357string(0) ""
358
359DONE
360