xref: /PHP-7.1/ext/standard/tests/strings/strstr.phpt (revision 7af945e2)
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 --
235bool(false)
236
237-- Iteration 10 --
238string(67) "-3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
239
240abcd$:Hello world"
241
242-- Iteration 11 --
243bool(false)
244
245-- Iteration 12 --
246string(40) "� abcd\xxyz �0 octal
247
248abcd$:Hello world"
249
250-- Iteration 13 --
251string(55) "NULL TRUE FALSE� abcd\xxyz �0 octal
252
253abcd$:Hello world"
254
255-- Iteration 14 --
256string(74) "012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
257
258abcd$:Hello world"
259
260-- Iteration 15 --
261string(40) "� abcd\xxyz �0 octal
262
263abcd$:Hello world"
264
265-- Iteration 16 --
266bool(false)
267
268-- Iteration 17 --
269string(50) "TRUE FALSE� abcd\xxyz �0 octal
270
271abcd$:Hello world"
272
273-- Iteration 18 --
274string(73) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
275
276abcd$:Hello world"
277
278-- Iteration 19 --
279bool(false)
280
281-- Iteration 20 --
282string(40) "� abcd\xxyz �0 octal
283
284abcd$:Hello world"
285
286-- Iteration 21 --
287string(45) "FALSE� abcd\xxyz �0 octal
288
289abcd$:Hello world"
290
291-- Iteration 22 --
292string(81) " world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
293
294abcd$:Hello world"
295
296-- Iteration 23 --
297string(60) "     NULL TRUE FALSE� abcd\xxyz �0 octal
298
299abcd$:Hello world"
300
301-- Iteration 24 --
302string(37) "bcd\xxyz �0 octal
303
304abcd$:Hello world"
305
306-- Iteration 25 --
307bool(false)
308
309-- Iteration 26 --
310string(20) "
311
312abcd$:Hello world"
313
314-- Iteration 27 --
315string(73) "12033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
316
317abcd$:Hello world"
318
319-- Iteration 28 --
320bool(false)
321
322-- Iteration 29 --
323string(86) "Hello world,012033 -3.3445     NULL TRUE FALSE� abcd\xxyz �0 octal
324
325abcd$:Hello world"
326
327*** Testing Miscelleneous input data ***
328-- Passing objects as string and needle --
329string(5) "world"
330
331-- passing an array as string and needle --
332
333Warning: strstr() expects parameter 1 to be string, array given in %s on line %d
334NULL
335string(27) "?world,!$%**()%**[][[[&@#~!"
336string(20) "!$%**()%**[][[[&@#~!"
337
338-- passing Resources as string and needle --
339
340Warning: strstr() expects parameter 1 to be string, resource given in %s on line %d
341NULL
342
343Warning: strstr() expects parameter 1 to be string, resource given in %s on line %d
344NULL
345
346-- Posiibilities with null --
347bool(false)
348bool(false)
349bool(false)
350string(1) "0"
351
352-- A longer and heredoc string --
353string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
354abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
355abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
356abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
357abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
358abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
359abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
360abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
361abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
362abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
363string(702) "123456789abcdefghijklmnopqrstuvwxyz0123456789
364abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
365abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
366abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
367abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
368abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
369abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
370abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
371abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
372abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
373
374-- A heredoc null string --
375bool(false)
376bool(false)
377bool(false)
378
379-- simple and complex syntax strings --
380string(5) "world"
381string(7) "world'S"
382
383Notice: Undefined variable: needleS in %s on line %d
384
385Warning: strstr(): Empty needle in %s on line %d
386bool(false)
387string(6) "worldS"
388string(6) "worldS"
389
390-- complex strings containing other than 7-bit chars --
391- Positions of some chars in the string '�������' are as follows -
392� => string(6) "������"
393� => string(1) "�"
394� => string(7) "�������"
395
396*** Testing error conditions ***
397Warning: strstr(): Empty needle in %s on line %d
398bool(false)
399
400Warning: strstr() expects at least 2 parameters, 0 given in %s on line %d
401NULL
402
403Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d
404NULL
405
406Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d
407NULL
408bool(false)
409
410Warning: strstr(): Empty needle in %s on line %d
411bool(false)
412
413Done
414