xref: /PHP-7.3/ext/standard/tests/strings/strstr.phpt (revision b746e698)
1--TEST--
2Test strstr() function
3--FILE--
4<?php
5/* Prototype: string strstr ( string $haystack, string $needle );
6   Description: Find first occurrence of a string
7   and reurns the rest of the string from that string
8*/
9
10echo "*** Testing basic functionality of strstr() ***\n";
11var_dump( strstr("test string", "test") );
12var_dump( strstr("test string", "string") );
13var_dump( strstr("test string", "strin") );
14var_dump( strstr("test string", "t s") );
15var_dump( strstr("test string", "g") );
16var_dump( md5(strstr("te".chr(0)."st", chr(0))) );
17var_dump( strstr("tEst", "test") );
18var_dump( strstr("teSt", "test") );
19var_dump( @strstr("", "") );
20var_dump( @strstr("a", "") );
21var_dump( @strstr("", "a") );
22
23
24echo "\n*** Testing strstr() with various needles ***";
25$string =
26"Hello world,012033 -3.3445     NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
27abcd$:Hello world";
28
29/* needles in an array to get the string starts with needle, in $string */
30$needles = array(
31  "Hello world",
32  "WORLD",
33  "\0",
34  "\x00",
35  "\x000",
36  "abcd",
37  "xyz",
38  "octal",
39  "-3",
40  -3,
41  "-3.344",
42  -3.344,
43  NULL,
44  "NULL",
45  "0",
46  0,
47  TRUE,
48  "TRUE",
49  "1",
50  1,
51  FALSE,
52  "FALSE",
53  " ",
54  "     ",
55  'b',
56  '\n',
57  "\n",
58  "12",
59  "12twelve",
60  $string
61);
62
63/* loop through to get the string starts with "needle" in $string */
64for( $i = 0; $i < count($needles); $i++ ) {
65  echo "\n-- Iteration $i --\n";
66  var_dump( strstr($string, $needles[$i]) );
67}
68
69
70echo "\n*** Testing Miscelleneous input data ***\n";
71
72echo "-- Passing objects as string and needle --\n";
73/* we get "Recoverable fatal error: saying Object of class needle could not be
74converted to string" by default when an object is passed instead of string:
75The error can be  avoided by choosing the __toString magix method as follows: */
76
77class stringable
78{
79  function __toString() {
80    return "Hello, world";
81  }
82}
83$obj_string = new stringable;
84
85class needle
86{
87  function __toString() {
88    return "world";
89  }
90}
91$obj_needle = new needle;
92
93var_dump(strstr("$obj_string", "$obj_needle"));
94
95
96echo "\n-- passing an array as string and needle --\n";
97$needles = array("hello", "?world", "!$%**()%**[][[[&@#~!");
98var_dump( strstr($needles, $needles) );  // won't work
99var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") );  // works
100var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") );  // works
101
102
103echo "\n-- passing Resources as string and needle --\n";
104$resource1 = fopen(__FILE__, "r");
105$resource2 = opendir(".");
106var_dump( strstr($resource1, $resource1) );
107var_dump( strstr($resource1, $resource2) );
108
109
110echo "\n-- Posiibilities with null --\n";
111var_dump( strstr("", NULL) );
112var_dump( strstr(NULL, NULL) );
113var_dump( strstr("a", NULL) );
114var_dump( strstr("/x0", "0") );  // Hexadecimal NUL
115
116echo "\n-- A longer and heredoc string --\n";
117$string = <<<EOD
118abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
119abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
120abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
121abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
122abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
123abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
124abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
125abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
126abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
127abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
128EOD;
129var_dump( strstr($string, "abcd") );
130var_dump( strstr($string, "1234") );
131
132echo "\n-- A heredoc null string --\n";
133$str = <<<EOD
134EOD;
135var_dump( strstr($str, "\0") );
136var_dump( strstr($str, NULL) );
137var_dump( strstr($str, "0") );
138
139
140echo "\n-- simple and complex syntax strings --\n";
141$needle = 'world';
142
143/* Simple syntax */
144var_dump( strstr("Hello, world", "$needle") );  // works
145var_dump( strstr("Hello, world'S", "$needle'S") );  // works
146var_dump( strstr("Hello, worldS", "$needleS") );  // won't work
147
148/* String with curly braces, complex syntax */
149var_dump( strstr("Hello, worldS", "${needle}S") );  // works
150var_dump( strstr("Hello, worldS", "{$needle}S") );  // works
151
152
153echo "\n-- complex strings containing other than 7-bit chars --\n";
154$str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
155echo "- Positions of some chars in the string '$str' are as follows -\n";
156echo chr(128)." => ";
157var_dump( strstr($str, chr(128)) );
158echo chr(255)." => ";
159var_dump( strstr($str, chr(255)) );
160echo chr(256)." => ";
161var_dump( strstr($str, chr(256)) );
162
163echo "\n*** Testing error conditions ***";
164var_dump( strstr($string, ""));
165var_dump( strstr() );  // zero argument
166var_dump( strstr("") );  // null argument
167var_dump( strstr($string) );  // without "needle"
168var_dump( strstr("a", "b", "c") );  // args > expected
169var_dump( strstr(NULL, "") );
170
171echo "\nDone";
172
173fclose($resource1);
174closedir($resource2);
175?>
176--EXPECTF--
177*** Testing basic functionality of strstr() ***
178string(11) "test string"
179string(6) "string"
180string(6) "string"
181string(8) "t string"
182string(1) "g"
183string(32) "7272696018bdeb2c9a3f8d01fc2a9273"
184bool(false)
185bool(false)
186bool(false)
187bool(false)
188bool(false)
189
190*** Testing strstr() with various needles ***
191-- Iteration 0 --
192string(86) "Hello world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
193
194abcd$:Hello world"
195
196-- Iteration 1 --
197bool(false)
198
199-- Iteration 2 --
200string(40) "� abcd\xxyz �0 octal
201
202abcd$:Hello world"
203
204-- Iteration 3 --
205string(40) "� abcd\xxyz �0 octal
206
207abcd$:Hello world"
208
209-- Iteration 4 --
210string(28) "�0 octal
211
212abcd$:Hello world"
213
214-- Iteration 5 --
215string(38) "abcd\xxyz �0 octal
216
217abcd$:Hello world"
218
219-- Iteration 6 --
220string(32) "xyz �0 octal
221
222abcd$:Hello world"
223
224-- Iteration 7 --
225string(25) "octal
226
227abcd$:Hello world"
228
229-- Iteration 8 --
230string(67) "-3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
231
232abcd$:Hello world"
233
234-- Iteration 9 --
235
236Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
237bool(false)
238
239-- Iteration 10 --
240string(67) "-3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
241
242abcd$:Hello world"
243
244-- Iteration 11 --
245
246Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
247bool(false)
248
249-- Iteration 12 --
250
251Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
252string(40) "� abcd\xxyz �0 octal
253
254abcd$:Hello world"
255
256-- Iteration 13 --
257string(55) "NULL TRUE FALSE� abcd\xxyz �0 octal
258
259abcd$:Hello world"
260
261-- Iteration 14 --
262string(74) "012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
263
264abcd$:Hello world"
265
266-- Iteration 15 --
267
268Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
269string(40) "� abcd\xxyz �0 octal
270
271abcd$:Hello world"
272
273-- Iteration 16 --
274
275Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
276bool(false)
277
278-- Iteration 17 --
279string(50) "TRUE FALSE� abcd\xxyz �0 octal
280
281abcd$:Hello world"
282
283-- Iteration 18 --
284string(73) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
285
286abcd$:Hello world"
287
288-- Iteration 19 --
289
290Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
291bool(false)
292
293-- Iteration 20 --
294
295Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
296string(40) "� abcd\xxyz �0 octal
297
298abcd$:Hello world"
299
300-- Iteration 21 --
301string(45) "FALSE� abcd\xxyz �0 octal
302
303abcd$:Hello world"
304
305-- Iteration 22 --
306string(81) " world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
307
308abcd$:Hello world"
309
310-- Iteration 23 --
311string(60) "     NULL TRUE FALSE� abcd\xxyz �0 octal
312
313abcd$:Hello world"
314
315-- Iteration 24 --
316string(37) "bcd\xxyz �0 octal
317
318abcd$:Hello world"
319
320-- Iteration 25 --
321bool(false)
322
323-- Iteration 26 --
324string(20) "
325
326abcd$:Hello world"
327
328-- Iteration 27 --
329string(73) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
330
331abcd$:Hello world"
332
333-- Iteration 28 --
334bool(false)
335
336-- Iteration 29 --
337string(86) "Hello world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
338
339abcd$:Hello world"
340
341*** Testing Miscelleneous input data ***
342-- Passing objects as string and needle --
343string(5) "world"
344
345-- passing an array as string and needle --
346
347Warning: strstr() expects parameter 1 to be string, array given in %s on line %d
348NULL
349string(27) "?world,!$%**()%**[][[[&@#~!"
350string(20) "!$%**()%**[][[[&@#~!"
351
352-- passing Resources as string and needle --
353
354Warning: strstr() expects parameter 1 to be string, resource given in %s on line %d
355NULL
356
357Warning: strstr() expects parameter 1 to be string, resource given in %s on line %d
358NULL
359
360-- Posiibilities with null --
361
362Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
363bool(false)
364
365Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
366bool(false)
367
368Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
369bool(false)
370string(1) "0"
371
372-- A longer and heredoc string --
373string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
374abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
375abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
376abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
377abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
378abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
379abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
380abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
381abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
382abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
383string(702) "123456789abcdefghijklmnopqrstuvwxyz0123456789
384abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
385abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
386abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
387abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
388abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
389abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
390abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
391abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
392abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
393
394-- A heredoc null string --
395bool(false)
396
397Deprecated: strstr(): Non-string needles will be interpreted as strings in %s on line %d
398bool(false)
399bool(false)
400
401-- simple and complex syntax strings --
402string(5) "world"
403string(7) "world'S"
404
405Notice: Undefined variable: needleS in %s on line %d
406
407Warning: strstr(): Empty needle in %s on line %d
408bool(false)
409string(6) "worldS"
410string(6) "worldS"
411
412-- complex strings containing other than 7-bit chars --
413- Positions of some chars in the string '�������' are as follows -
414� => string(6) "������"
415� => string(1) "�"
416� => string(7) "�������"
417
418*** Testing error conditions ***
419Warning: strstr(): Empty needle in %s on line %d
420bool(false)
421
422Warning: strstr() expects at least 2 parameters, 0 given in %s on line %d
423NULL
424
425Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d
426NULL
427
428Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d
429NULL
430bool(false)
431
432Warning: strstr(): Empty needle in %s on line %d
433bool(false)
434
435Done
436