xref: /PHP-8.2/ext/standard/tests/strings/strpos.phpt (revision 9a90bd70)
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--
178Deprecated: Using ${var} in strings is deprecated, use {$var} instead in %s on line %d
179*** Testing basic functionality of strpos() ***
180int(0)
181int(5)
182int(5)
183int(3)
184int(10)
185int(2)
186bool(false)
187bool(false)
188int(0)
189int(0)
190bool(false)
191int(1)
192
193*** Testing strpos() to find various needles and a long string ***
194Position of 'Hello world' is => int(0)
195Position of 'WORLD' is => bool(false)
196Position of '%0' is => int(46)
197Position of '%0' is => int(46)
198Position of '%00' is => int(58)
199Position of 'abcd' is => int(48)
200Position of 'xyz' is => int(54)
201Position of 'octal' is => int(61)
202Position of '-3' is => int(19)
203Position of '-3' is => int(19)
204Position of '-3.344' is => int(19)
205Position of '-3.344' is => int(19)
206Position of 'NULL' is => int(31)
207Position of '0' is => int(12)
208Position of '0' is => int(12)
209Position of '1' is => int(13)
210Position of 'TRUE' is => int(36)
211Position of '1' is => int(13)
212Position of '1' is => int(13)
213Position of '' is => int(0)
214Position of 'FALSE' is => int(41)
215Position of ' ' is => int(5)
216Position of '     ' is => int(26)
217Position of 'b' is => int(49)
218Position of '\n' is => bool(false)
219Position of '
220' is => int(66)
221Position of '12' is => int(13)
222Position of '12twelve' is => bool(false)
223Position of 'Hello world,012033 -3.3445     NULL TRUE FALSE%0 abcd\xxyz %00 octal
224
225abcd$:Hello world' is => int(0)
226
227*** Testing strpos() with possible variations in offset ***
228Position of 'Hello' with offset '1' is => int(74)
229Position of 'Hello' with offset 'string' is =>
230strpos(): Argument #3 ($offset) must be of type int, string given
231Position of 'Hello' with offset '' is =>
232strpos(): Argument #3 ($offset) must be of type int, string given
233Position of 'Hello' with offset '0' is => int(0)
234Position of 'Hello' with offset '1' is => int(74)
235Position of 'Hello' with offset '' is => int(0)
236Position of 'Hello' with offset 'string12' is =>
237strpos(): Argument #3 ($offset) must be of type int, string given
238Position of 'Hello' with offset '-10' is => bool(false)
239Position of 'Hello' with offset '-15' is => int(74)
240Position of 'Hello' with offset '-85' is => int(0)
241
242*** Testing miscellaneous input data ***
243-- Passing objects as string and needle --
244int(7)
245
246-- Posiibilities with null --
247
248Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
249int(0)
250
251Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in %s on line %d
252
253Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
254int(0)
255
256Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in %s on line %d
257int(0)
258int(2)
259
260-- A longer and heredoc string --
261int(0)
262int(73)
263int(73)
264int(728)
265
266-- A heredoc null string --
267bool(false)
268bool(false)
269
270-- simple and complex syntax strings --
271int(7)
272int(7)
273
274Warning: Undefined variable $needleS in %s on line %d
275int(0)
276int(7)
277int(7)
278
279*** Testing error conditions ***
280int(0)
281strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
282strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
283
284DONE
285