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