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