Annotation of embedaddon/php/ext/standard/tests/strings/strpos.phpt, revision 1.1.1.2
1.1 misho 1: --TEST--
2: Test strpos() function
3: --INI--
4: precision=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:
11: echo "*** Testing basic functionality of strpos() ***\n";
12: var_dump( strpos("test string", "test") );
13: var_dump( strpos("test string", "string") );
14: var_dump( strpos("test string", "strin") );
15: var_dump( strpos("test string", "t s") );
16: var_dump( strpos("test string", "g") );
17: var_dump( strpos("te".chr(0)."st", chr(0)) );
18: var_dump( strpos("tEst", "test") );
19: var_dump( strpos("teSt", "test") );
20: var_dump( @strpos("", "") );
21: var_dump( @strpos("a", "") );
22: var_dump( @strpos("", "a") );
23: var_dump( @strpos("\\\\a", "\\a") );
24:
25:
26: echo "\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
29: abcd$: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 */
66: for( $i = 0; $i < count($needles); $i++ ) {
67: echo "Position of '$needles[$i]' is => ";
68: var_dump( strpos($string, $needles[$i]) );
69: }
70:
71:
72: echo "\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: );
86:
87: /* loop through to get the "needle" position in $string */
88: for( $i = 0; $i < count( $offset_values ); $i++ ) {
89: echo "Position of 'Hello' with offset '$offset_values[$i]' is => ";
90: var_dump( strpos($string, "Hello", $offset_values[$i]) );
91: }
92:
93:
94: echo "\n*** Testing Miscelleneous input data ***\n";
95:
96: echo "-- Passing objects as string and needle --\n";
97: /* we get "Catchable fatal error: saying Object of class needle could not be
98: converted to string" by default when an object is passed instead of string:
99: The error can be avoided by chosing the __toString magix method as follows: */
100:
101: class string
102: {
103: function __toString() {
104: return "Hello, world";
105: }
106: }
107: $obj_string = new string;
108:
109: class needle
110: {
111: function __toString() {
112: return "world";
113: }
114: }
115: $obj_needle = new needle;
116:
117: var_dump( strpos("$obj_string", "$obj_needle") );
118:
119: echo "\n-- Passing an array as string and needle --\n";
120: $needles = array("hello", "?world", "!$%**()%**[][[[&@#~!");
121: var_dump( strpos($needles, $needles) ); // won't work
122: var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works
123: var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works
124:
125:
126: echo "\n-- Passing Resources as string and needle --\n";
127: $resource1 = fopen(__FILE__, "r");
128: $resource2 = opendir(".");
129: var_dump( strpos($resource1, $resource1) );
130: var_dump( strpos($resource1, $resource2) );
131:
132: echo "\n-- Posiibilities with null --\n";
133: var_dump( strpos("", NULL) );
134: var_dump( strpos(NULL, NULL) );
135: var_dump( strpos("a", NULL) );
136: var_dump( strpos("/x0", "0") ); // Hexadecimal NUL
137:
138: echo "\n-- A longer and heredoc string --\n";
139: $string = <<<EOD
140: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
141: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
142: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
143: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
144: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
145: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
146: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
147: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
148: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
149: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
150: EOD;
151: var_dump( strpos($string, "abcd") );
152: var_dump( strpos($string, "abcd", 72) ); // 72 -> "\n" in the first line
153: var_dump( strpos($string, "abcd", 73) ); // 73 -> "abcd" in the second line
154: var_dump( strpos($string, "9", (strlen($string)-1)) );
155:
156: echo "\n-- A heredoc null string --\n";
157: $str = <<<EOD
158: EOD;
159: var_dump( strpos($str, "\0") );
160: var_dump( strpos($str, NULL) );
161: var_dump( strpos($str, "0") );
162:
163:
164: echo "\n-- simple and complex syntax strings --\n";
165: $needle = 'world';
166:
167: /* Simple syntax */
168: var_dump( strpos("Hello, world", "$needle") ); // works
169: var_dump( strpos("Hello, world'S", "$needle'S") ); // works
170: var_dump( strpos("Hello, worldS", "$needleS") ); // won't work
171:
172: /* String with curly braces, complex syntax */
173: var_dump( strpos("Hello, worldS", "${needle}S") ); // works
174: var_dump( strpos("Hello, worldS", "{$needle}S") ); // works
175:
176:
177: echo "\n-- complex strings containing other than 7-bit chars --\n";
178: $str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
179: echo "-- Positions of some chars in the string '$str' are as follows --\n";
180: echo chr(128)." => ";
181: var_dump( strpos($str, chr(128)) );
182: echo chr(255)." => ";
183: var_dump( strpos($str, chr(255), 3) );
184: echo chr(256)." => ";
185: var_dump( strpos($str, chr(256)) );
186:
187: echo "\n*** Testing error conditions ***";
188: var_dump( strpos($string, "") );
189: var_dump( strpos() ); // zero argument
190: var_dump( strpos("") ); // null argument
191: var_dump( strpos($string) ); // without "needle"
192: var_dump( strpos("a", "b", "c", "d") ); // args > expected
193: var_dump( strpos($string, "test", strlen($string)+1) ); // offset > strlen()
194: var_dump( strpos($string, "test", -1) ); // offset < 0
195: var_dump( strpos(NULL, "") );
196:
197: echo "\nDone";
198:
199: fclose($resource1);
200: closedir($resource2);
201: ?>
202: --EXPECTF--
203: *** Testing basic functionality of strpos() ***
204: int(0)
205: int(5)
206: int(5)
207: int(3)
208: int(10)
209: int(2)
210: bool(false)
211: bool(false)
212: bool(false)
213: bool(false)
214: bool(false)
215: int(1)
216:
217: *** Testing strpos() to find various needles and a long string ***
218: Position of 'Hello world' is => int(0)
219: Position of 'WORLD' is => bool(false)
220: Position of ' ' is => int(46)
221: Position of ' ' is => int(46)
222: Position of ' 0' is => int(58)
223: Position of 'abcd' is => int(48)
224: Position of 'xyz' is => int(54)
225: Position of 'octal' is => int(61)
226: Position of '-3' is => int(19)
227: Position of '-3' is => bool(false)
228: Position of '-3.344' is => int(19)
229: Position of '-3.344' is => bool(false)
230: Position of '' is => int(46)
231: Position of 'NULL' is => int(31)
232: Position of '0' is => int(12)
233: Position of '0' is => int(46)
234: Position of '1' is => bool(false)
235: Position of 'TRUE' is => int(36)
236: Position of '1' is => int(13)
237: Position of '1' is => bool(false)
238: Position of '' is => int(46)
239: Position of 'FALSE' is => int(41)
240: Position of ' ' is => int(5)
241: Position of ' ' is => int(26)
242: Position of 'b' is => int(49)
243: Position of '\n' is => bool(false)
244: Position of '
245: ' is => int(66)
246: Position of '12' is => int(13)
247: Position of '12twelve' is => bool(false)
248: Position of 'Hello world,012033 -3.3445 NULL TRUE FALSE abcd\xxyz 0 octal
249:
250: abcd$:Hello world' is => int(0)
251:
252: *** Testing strpos() with possible variations in offset ***
253: Position of 'Hello' with offset '1' is => int(74)
254: Position of 'Hello' with offset 'string' is =>
255: Warning: strpos() expects parameter 3 to be long, string given in %s on line %d
256: NULL
257: Position of 'Hello' with offset '' is => int(0)
258: Position of 'Hello' with offset '' is =>
259: Warning: strpos() expects parameter 3 to be long, string given in %s on line %d
260: NULL
261: Position of 'Hello' with offset '12string' is =>
262: Notice: A non well formed numeric value encountered in %s on line %d
263: int(74)
264: Position of 'Hello' with offset '0' is => int(0)
265: Position of 'Hello' with offset '1' is => int(74)
266: Position of 'Hello' with offset '' is => int(0)
267: Position of 'Hello' with offset '' is => int(0)
268: Position of 'Hello' with offset 'string12' is =>
269: Warning: strpos() expects parameter 3 to be long, string given in %s on line %d
270: NULL
271: Position of 'Hello' with offset '12.3string' is =>
272: Notice: A non well formed numeric value encountered in %s on line %d
273: int(74)
274:
275: *** Testing Miscelleneous input data ***
276: -- Passing objects as string and needle --
277: int(7)
278:
279: -- Passing an array as string and needle --
280:
281: Warning: strpos() expects parameter 1 to be string, array given in %s on line %d
282: NULL
283: int(5)
284: int(12)
285:
286: -- Passing Resources as string and needle --
287:
288: Warning: strpos() expects parameter 1 to be string, resource given in %s on line %d
289: NULL
290:
291: Warning: strpos() expects parameter 1 to be string, resource given in %s on line %d
292: NULL
293:
294: -- Posiibilities with null --
295: bool(false)
296: bool(false)
297: bool(false)
298: int(2)
299:
300: -- A longer and heredoc string --
301: int(0)
302: int(73)
303: int(73)
304: int(728)
305:
306: -- A heredoc null string --
307: bool(false)
308: bool(false)
309: bool(false)
310:
311: -- simple and complex syntax strings --
312: int(7)
313: int(7)
314:
315: Notice: Undefined variable: needleS in %s on line %d
316:
1.1.1.2 ! misho 317: Warning: strpos(): Empty needle in %s on line %d
1.1 misho 318: bool(false)
319: int(7)
320: int(7)
321:
322: -- complex strings containing other than 7-bit chars --
323: -- Positions of some chars in the string ' ' are as follows --
324: => int(1)
325: => int(6)
326: