xref: /PHP-8.1/ext/standard/tests/strings/strpos.phpt (revision ea256a21)
1--TEST--
2Test strpos() function
3--INI--
4precision=14
5--FILE--
6<?php
7echo "*** Testing basic functionality of strpos() ***\n";
8var_dump( strpos("test string", "test") );
9var_dump( strpos("test string", "string") );
10var_dump( strpos("test string", "strin") );
11var_dump( strpos("test string", "t s") );
12var_dump( strpos("test string", "g") );
13var_dump( strpos("te".chr(0)."st", chr(0)) );
14var_dump( strpos("tEst", "test") );
15var_dump( strpos("teSt", "test") );
16var_dump( strpos("", "") );
17var_dump( strpos("a", "") );
18var_dump( strpos("", "a") );
19var_dump( strpos("\\\\a", "\\a") );
20
21
22echo "\n*** Testing strpos() to find various needles and a long string ***\n";
23$string =
24"Hello world,012033 -3.3445     NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
25abcd$:Hello world";
26
27/* needles in an array to get the position of needle in $string */
28$needles = array(
29  "Hello world",
30  "WORLD",
31  "\0",
32  "\x00",
33  "\x000",
34  "abcd",
35  "xyz",
36  "octal",
37  "-3",
38  -3,
39  "-3.344",
40  -3.344,
41  "NULL",
42  "0",
43  0,
44  TRUE,
45  "TRUE",
46  "1",
47  1,
48  FALSE,
49  "FALSE",
50  " ",
51  "     ",
52  'b',
53  '\n',
54  "\n",
55  "12",
56  "12twelve",
57  $string
58);
59
60/* loop through to get the "needle" position in $string */
61for( $i = 0; $i < count($needles); $i++ ) {
62  echo "Position of '$needles[$i]' is => ";
63  var_dump( strpos($string, $needles[$i]) );
64}
65
66
67echo "\n*** Testing strpos() with possible variations in offset ***\n";
68$offset_values = array (
69  1,  // offset = 1
70  "string",  // offset as string, converts to zero
71  "",  // offset as string, converts to zero
72  "0",
73  TRUE,
74  FALSE,
75  "string12",
76  -10, // Not found
77  -15, // Found
78  -strlen($string),
79);
80
81/* loop through to get the "needle" position in $string */
82for( $i = 0; $i < count( $offset_values ); $i++ ) {
83  echo "Position of 'Hello' with offset '$offset_values[$i]' is => ";
84  try {
85    var_dump( strpos($string, "Hello", $offset_values[$i]) );
86  } catch (TypeError $e) {
87    echo "\n", $e->getMessage(), "\n";
88  }
89}
90
91
92echo "\n*** Testing miscellaneous input data ***\n";
93
94echo "-- Passing objects as string and needle --\n";
95/* we get "Recoverable fatal error: saying Object of class needle could not be
96 converted to string" by default when an object is passed instead of string:
97 The error can be avoided by choosing the __toString magix method as follows: */
98
99class StringCapable
100{
101  function __toString() {
102    return "Hello, world";
103  }
104}
105$obj_string = new StringCapable;
106
107class needle
108{
109  function __toString() {
110    return "world";
111  }
112}
113$obj_needle = new needle;
114
115var_dump( strpos("$obj_string", "$obj_needle") );
116
117echo "\n-- Posiibilities with null --\n";
118var_dump( strpos("", NULL) );
119var_dump( strpos(NULL, NULL) );
120var_dump( strpos("a", NULL) );
121var_dump( strpos("/x0", "0") );	 // Hexadecimal NUL
122
123echo "\n-- A longer and heredoc string --\n";
124$string = <<<EOD
125abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
126abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
127abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
128abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
129abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
130abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
131abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
132abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
133abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
134abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
135EOD;
136var_dump( strpos($string, "abcd") );
137var_dump( strpos($string, "abcd", 72) );  // 72 -> "\n" in the first line
138var_dump( strpos($string, "abcd", 73) );  // 73 -> "abcd" in the second line
139var_dump( strpos($string, "9", (strlen($string)-1)) );
140
141echo "\n-- A heredoc null string --\n";
142$str = <<<EOD
143EOD;
144var_dump( strpos($str, "\0") );
145var_dump( strpos($str, "0") );
146
147
148echo "\n-- simple and complex syntax strings --\n";
149$needle = 'world';
150
151/* Simple syntax */
152var_dump( strpos("Hello, world", "$needle") );  // works
153var_dump( strpos("Hello, world'S", "$needle'S") );  // works
154var_dump( strpos("Hello, worldS", "$needleS") );  // won't work
155
156/* String with curly braces, complex syntax */
157var_dump( strpos("Hello, worldS", "${needle}S") );  // works
158var_dump( strpos("Hello, worldS", "{$needle}S") );  // works
159
160echo "\n*** Testing error conditions ***\n";
161var_dump( strpos($string, "") );
162try {
163    strpos($string, "test", strlen($string)+1);  // offset > strlen()
164} catch (ValueError $exception) {
165    echo $exception->getMessage() . "\n";
166}
167
168try {
169    strpos($string, "test", -strlen($string)-1);  // offset before start
170} catch (ValueError $exception) {
171    echo $exception->getMessage() . "\n";
172}
173
174?>
175
176DONE
177--EXPECTF--
178*** Testing basic functionality of strpos() ***
179int(0)
180int(5)
181int(5)
182int(3)
183int(10)
184int(2)
185bool(false)
186bool(false)
187int(0)
188int(0)
189bool(false)
190int(1)
191
192*** Testing strpos() to find various needles and a long string ***
193Position of 'Hello world' is => int(0)
194Position of 'WORLD' is => bool(false)
195Position of '%0' is => int(46)
196Position of '%0' is => int(46)
197Position of '%00' is => int(58)
198Position of 'abcd' is => int(48)
199Position of 'xyz' is => int(54)
200Position of 'octal' is => int(61)
201Position of '-3' is => int(19)
202Position of '-3' is => int(19)
203Position of '-3.344' is => int(19)
204Position of '-3.344' is => int(19)
205Position of 'NULL' is => int(31)
206Position of '0' is => int(12)
207Position of '0' is => int(12)
208Position of '1' is => int(13)
209Position of 'TRUE' is => int(36)
210Position of '1' is => int(13)
211Position of '1' is => int(13)
212Position of '' is => int(0)
213Position of 'FALSE' is => int(41)
214Position of ' ' is => int(5)
215Position of '     ' is => int(26)
216Position of 'b' is => int(49)
217Position of '\n' is => bool(false)
218Position of '
219' is => int(66)
220Position of '12' is => int(13)
221Position of '12twelve' is => bool(false)
222Position of 'Hello world,012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
223
224abcd$:Hello world' is => int(0)
225
226*** Testing strpos() with possible variations in offset ***
227Position of 'Hello' with offset '1' is => int(74)
228Position of 'Hello' with offset 'string' is =>
229strpos(): Argument #3 ($offset) must be of type int, string given
230Position of 'Hello' with offset '' is =>
231strpos(): Argument #3 ($offset) must be of type int, string given
232Position of 'Hello' with offset '0' is => int(0)
233Position of 'Hello' with offset '1' is => int(74)
234Position of 'Hello' with offset '' is => int(0)
235Position of 'Hello' with offset 'string12' is =>
236strpos(): Argument #3 ($offset) must be of type int, string given
237Position of 'Hello' with offset '-10' is => bool(false)
238Position of 'Hello' with offset '-15' is => int(74)
239Position of 'Hello' with offset '-85' is => int(0)
240
241*** Testing miscellaneous input data ***
242-- Passing objects as string and needle --
243int(7)
244
245-- Posiibilities with null --
246
247Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
248int(0)
249
250Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in %s on line %d
251
252Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
253int(0)
254
255Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
256int(0)
257int(2)
258
259-- A longer and heredoc string --
260int(0)
261int(73)
262int(73)
263int(728)
264
265-- A heredoc null string --
266bool(false)
267bool(false)
268
269-- simple and complex syntax strings --
270int(7)
271int(7)
272
273Warning: Undefined variable $needleS in %s on line %d
274int(0)
275int(7)
276int(7)
277
278*** Testing error conditions ***
279int(0)
280strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
281strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
282
283DONE
284