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 => bool(false) 231Position of '-3.344' is => int(19) 232Position of '-3.344' is => bool(false) 233Position of '' is => int(46) 234Position of 'NULL' is => int(31) 235Position of '0' is => int(12) 236Position of '0' is => int(46) 237Position of '1' is => bool(false) 238Position of 'TRUE' is => int(36) 239Position of '1' is => int(13) 240Position of '1' is => bool(false) 241Position of '' is => int(46) 242Position of 'FALSE' is => int(41) 243Position of ' ' is => int(5) 244Position of ' ' is => int(26) 245Position of 'b' is => int(49) 246Position of '\n' is => bool(false) 247Position of ' 248' is => int(66) 249Position of '12' is => int(13) 250Position of '12twelve' is => bool(false) 251Position of 'Hello world,012033 -3.3445 NULL TRUE FALSE abcd\xxyz 0 octal 252 253abcd$:Hello world' is => int(0) 254 255*** Testing strpos() with possible variations in offset *** 256Position of 'Hello' with offset '1' is => int(74) 257Position of 'Hello' with offset 'string' is => 258Warning: strpos() expects parameter 3 to be integer, string given in %s on line %d 259NULL 260Position of 'Hello' with offset '' is => int(0) 261Position of 'Hello' with offset '' is => 262Warning: strpos() expects parameter 3 to be integer, string given in %s on line %d 263NULL 264Position of 'Hello' with offset '12string' is => 265Notice: A non well formed numeric value encountered in %s on line %d 266int(74) 267Position of 'Hello' with offset '0' is => int(0) 268Position of 'Hello' with offset '1' is => int(74) 269Position of 'Hello' with offset '' is => int(0) 270Position of 'Hello' with offset '' is => int(0) 271Position of 'Hello' with offset 'string12' is => 272Warning: strpos() expects parameter 3 to be integer, string given in %s on line %d 273NULL 274Position of 'Hello' with offset '12.3string' is => 275Notice: A non well formed numeric value encountered in %s on line %d 276int(74) 277Position of 'Hello' with offset '-10' is => bool(false) 278Position of 'Hello' with offset '-15' is => int(74) 279Position of 'Hello' with offset '-85' is => int(0) 280 281*** Testing Miscelleneous input data *** 282-- Passing objects as string and needle -- 283int(7) 284 285-- Passing an array as string and needle -- 286 287Warning: strpos() expects parameter 1 to be string, array given in %s on line %d 288NULL 289int(5) 290int(12) 291 292-- Passing Resources as string and needle -- 293 294Warning: strpos() expects parameter 1 to be string, resource given in %s on line %d 295NULL 296 297Warning: strpos() expects parameter 1 to be string, resource given in %s on line %d 298NULL 299 300-- Posiibilities with null -- 301bool(false) 302bool(false) 303bool(false) 304int(2) 305 306-- A longer and heredoc string -- 307int(0) 308int(73) 309int(73) 310int(728) 311 312-- A heredoc null string -- 313bool(false) 314bool(false) 315bool(false) 316 317-- simple and complex syntax strings -- 318int(7) 319int(7) 320 321Notice: Undefined variable: needleS in %s on line %d 322 323Warning: strpos(): Empty needle in %s on line %d 324bool(false) 325int(7) 326int(7) 327 328-- complex strings containing other than 7-bit chars -- 329-- Positions of some chars in the string '������' are as follows -- 330� => int(1) 331� => int(6) 332 => int(0) 333 334*** Testing error conditions *** 335Warning: strpos(): Empty needle in %s on line %d 336bool(false) 337 338Warning: strpos() expects at least 2 parameters, 0 given in %s on line %d 339NULL 340 341Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d 342NULL 343 344Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d 345NULL 346 347Warning: strpos() expects at most 3 parameters, 4 given in %s on line %d 348NULL 349 350Warning: strpos(): Offset not contained in string in %s on line %d 351bool(false) 352 353Warning: strpos(): Offset not contained in string in %s on line %d 354bool(false) 355 356Warning: strpos(): Empty needle in %s on line %d 357bool(false) 358 359Done 360