xref: /PHP-8.4/ext/standard/tests/strings/strstr.phpt (revision 9a90bd70)
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--
139Deprecated: Using ${var} in strings is deprecated, use {$var} instead in %s on line %d
140*** Testing basic functionality of strstr() ***
141string(11) "test string"
142string(6) "string"
143string(6) "string"
144string(8) "t string"
145string(1) "g"
146string(32) "7272696018bdeb2c9a3f8d01fc2a9273"
147bool(false)
148bool(false)
149string(0) ""
150string(1) "a"
151bool(false)
152
153*** Testing strstr() with various needles ***
154-- Iteration 0 --
155string(85) "Hello world,012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
156
157abcd$:Hello world"
158
159-- Iteration 1 --
160bool(false)
161
162-- Iteration 2 --
163string(39) "%0 abcd\xxyz %00 octal
164
165abcd$:Hello world"
166
167-- Iteration 3 --
168string(39) "%0 abcd\xxyz %00 octal
169
170abcd$:Hello world"
171
172-- Iteration 4 --
173string(27) "%00 octal
174
175abcd$:Hello world"
176
177-- Iteration 5 --
178string(37) "abcd\xxyz %00 octal
179
180abcd$:Hello world"
181
182-- Iteration 6 --
183string(31) "xyz %00 octal
184
185abcd$:Hello world"
186
187-- Iteration 7 --
188string(24) "octal
189
190abcd$:Hello world"
191
192-- Iteration 8 --
193string(66) "-3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
194
195abcd$:Hello world"
196
197-- Iteration 9 --
198string(66) "-3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
199
200abcd$:Hello world"
201
202-- Iteration 10 --
203string(66) "-3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
204
205abcd$:Hello world"
206
207-- Iteration 11 --
208string(66) "-3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
209
210abcd$:Hello world"
211
212-- Iteration 12 --
213string(54) "NULL TRUE FALSE%0 abcd\xxyz %00 octal
214
215abcd$:Hello world"
216
217-- Iteration 13 --
218string(73) "012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
219
220abcd$:Hello world"
221
222-- Iteration 14 --
223string(73) "012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
224
225abcd$:Hello world"
226
227-- Iteration 15 --
228string(72) "12033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
229
230abcd$:Hello world"
231
232-- Iteration 16 --
233string(49) "TRUE FALSE%0 abcd\xxyz %00 octal
234
235abcd$:Hello world"
236
237-- Iteration 17 --
238string(72) "12033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
239
240abcd$:Hello world"
241
242-- Iteration 18 --
243string(72) "12033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
244
245abcd$:Hello world"
246
247-- Iteration 19 --
248string(85) "Hello world,012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
249
250abcd$:Hello world"
251
252-- Iteration 20 --
253string(44) "FALSE%0 abcd\xxyz %00 octal
254
255abcd$:Hello world"
256
257-- Iteration 21 --
258string(80) " world,012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
259
260abcd$:Hello world"
261
262-- Iteration 22 --
263string(59) "     NULL TRUE FALSE%0 abcd\xxyz %00 octal
264
265abcd$:Hello world"
266
267-- Iteration 23 --
268string(36) "bcd\xxyz %00 octal
269
270abcd$:Hello world"
271
272-- Iteration 24 --
273bool(false)
274
275-- Iteration 25 --
276string(19) "
277
278abcd$:Hello world"
279
280-- Iteration 26 --
281string(72) "12033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
282
283abcd$:Hello world"
284
285-- Iteration 27 --
286bool(false)
287
288-- Iteration 28 --
289string(85) "Hello world,012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
290
291abcd$:Hello world"
292
293*** Testing miscellaneous input data ***
294-- Passing objects as string and needle --
295string(5) "world"
296
297-- Posiibilities with null --
298
299Deprecated: strstr(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
300string(0) ""
301
302Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in %s on line %d
303
304Deprecated: strstr(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
305string(0) ""
306
307Deprecated: strstr(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
308string(1) "a"
309string(1) "0"
310
311-- A longer and heredoc string --
312string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
313abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
314abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
315abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
316abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
317abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
318abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
319abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
320abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
321abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
322string(702) "123456789abcdefghijklmnopqrstuvwxyz0123456789
323abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
324abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
325abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
326abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
327abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
328abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
329abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
330abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
331abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
332
333-- A heredoc null string --
334bool(false)
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)
357
358DONE
359