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); 86 87/* loop through to get the "needle" position in $string */ 88for( $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 94echo "\n*** Testing Miscelleneous input data ***\n"; 95 96echo "-- 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 101class string 102{ 103 function __toString() { 104 return "Hello, world"; 105 } 106} 107$obj_string = new string; 108 109class needle 110{ 111 function __toString() { 112 return "world"; 113 } 114} 115$obj_needle = new needle; 116 117var_dump( strpos("$obj_string", "$obj_needle") ); 118 119echo "\n-- Passing an array as string and needle --\n"; 120$needles = array("hello", "?world", "!$%**()%**[][[[&@#~!"); 121var_dump( strpos($needles, $needles) ); // won't work 122var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works 123var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works 124 125 126echo "\n-- Passing Resources as string and needle --\n"; 127$resource1 = fopen(__FILE__, "r"); 128$resource2 = opendir("."); 129var_dump( strpos($resource1, $resource1) ); 130var_dump( strpos($resource1, $resource2) ); 131 132echo "\n-- Posiibilities with null --\n"; 133var_dump( strpos("", NULL) ); 134var_dump( strpos(NULL, NULL) ); 135var_dump( strpos("a", NULL) ); 136var_dump( strpos("/x0", "0") ); // Hexadecimal NUL 137 138echo "\n-- A longer and heredoc string --\n"; 139$string = <<<EOD 140abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 141abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 142abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 143abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 144abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 145abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 146abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 147abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 148abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 149abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 150EOD; 151var_dump( strpos($string, "abcd") ); 152var_dump( strpos($string, "abcd", 72) ); // 72 -> "\n" in the first line 153var_dump( strpos($string, "abcd", 73) ); // 73 -> "abcd" in the second line 154var_dump( strpos($string, "9", (strlen($string)-1)) ); 155 156echo "\n-- A heredoc null string --\n"; 157$str = <<<EOD 158EOD; 159var_dump( strpos($str, "\0") ); 160var_dump( strpos($str, NULL) ); 161var_dump( strpos($str, "0") ); 162 163 164echo "\n-- simple and complex syntax strings --\n"; 165$needle = 'world'; 166 167/* Simple syntax */ 168var_dump( strpos("Hello, world", "$needle") ); // works 169var_dump( strpos("Hello, world'S", "$needle'S") ); // works 170var_dump( strpos("Hello, worldS", "$needleS") ); // won't work 171 172/* String with curly braces, complex syntax */ 173var_dump( strpos("Hello, worldS", "${needle}S") ); // works 174var_dump( strpos("Hello, worldS", "{$needle}S") ); // works 175 176 177echo "\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); 179echo "-- Positions of some chars in the string '$str' are as follows --\n"; 180echo chr(128)." => "; 181var_dump( strpos($str, chr(128)) ); 182echo chr(255)." => "; 183var_dump( strpos($str, chr(255), 3) ); 184echo chr(256)." => "; 185var_dump( strpos($str, chr(256)) ); 186 187echo "\n*** Testing error conditions ***"; 188var_dump( strpos($string, "") ); 189var_dump( strpos() ); // zero argument 190var_dump( strpos("") ); // null argument 191var_dump( strpos($string) ); // without "needle" 192var_dump( strpos("a", "b", "c", "d") ); // args > expected 193var_dump( strpos($string, "test", strlen($string)+1) ); // offset > strlen() 194var_dump( strpos($string, "test", -1) ); // offset < 0 195var_dump( strpos(NULL, "") ); 196 197echo "\nDone"; 198 199fclose($resource1); 200closedir($resource2); 201?> 202--EXPECTF-- 203*** Testing basic functionality of strpos() *** 204int(0) 205int(5) 206int(5) 207int(3) 208int(10) 209int(2) 210bool(false) 211bool(false) 212bool(false) 213bool(false) 214bool(false) 215int(1) 216 217*** Testing strpos() to find various needles and a long string *** 218Position of 'Hello world' is => int(0) 219Position of 'WORLD' is => bool(false) 220Position of '' is => int(46) 221Position of '' is => int(46) 222Position of '0' is => int(58) 223Position of 'abcd' is => int(48) 224Position of 'xyz' is => int(54) 225Position of 'octal' is => int(61) 226Position of '-3' is => int(19) 227Position of '-3' is => bool(false) 228Position of '-3.344' is => int(19) 229Position of '-3.344' is => bool(false) 230Position of '' is => int(46) 231Position of 'NULL' is => int(31) 232Position of '0' is => int(12) 233Position of '0' is => int(46) 234Position of '1' is => bool(false) 235Position of 'TRUE' is => int(36) 236Position of '1' is => int(13) 237Position of '1' is => bool(false) 238Position of '' is => int(46) 239Position of 'FALSE' is => int(41) 240Position of ' ' is => int(5) 241Position of ' ' is => int(26) 242Position of 'b' is => int(49) 243Position of '\n' is => bool(false) 244Position of ' 245' is => int(66) 246Position of '12' is => int(13) 247Position of '12twelve' is => bool(false) 248Position of 'Hello world,012033 -3.3445 NULL TRUE FALSE abcd\xxyz 0 octal 249 250abcd$:Hello world' is => int(0) 251 252*** Testing strpos() with possible variations in offset *** 253Position of 'Hello' with offset '1' is => int(74) 254Position of 'Hello' with offset 'string' is => 255Warning: strpos() expects parameter 3 to be long, string given in %s on line %d 256NULL 257Position of 'Hello' with offset '' is => int(0) 258Position of 'Hello' with offset '' is => 259Warning: strpos() expects parameter 3 to be long, string given in %s on line %d 260NULL 261Position of 'Hello' with offset '12string' is => 262Notice: A non well formed numeric value encountered in %s on line %d 263int(74) 264Position of 'Hello' with offset '0' is => int(0) 265Position of 'Hello' with offset '1' is => int(74) 266Position of 'Hello' with offset '' is => int(0) 267Position of 'Hello' with offset '' is => int(0) 268Position of 'Hello' with offset 'string12' is => 269Warning: strpos() expects parameter 3 to be long, string given in %s on line %d 270NULL 271Position of 'Hello' with offset '12.3string' is => 272Notice: A non well formed numeric value encountered in %s on line %d 273int(74) 274 275*** Testing Miscelleneous input data *** 276-- Passing objects as string and needle -- 277int(7) 278 279-- Passing an array as string and needle -- 280 281Warning: strpos() expects parameter 1 to be string, array given in %s on line %d 282NULL 283int(5) 284int(12) 285 286-- Passing Resources as string and needle -- 287 288Warning: strpos() expects parameter 1 to be string, resource given in %s on line %d 289NULL 290 291Warning: strpos() expects parameter 1 to be string, resource given in %s on line %d 292NULL 293 294-- Posiibilities with null -- 295bool(false) 296bool(false) 297bool(false) 298int(2) 299 300-- A longer and heredoc string -- 301int(0) 302int(73) 303int(73) 304int(728) 305 306-- A heredoc null string -- 307bool(false) 308bool(false) 309bool(false) 310 311-- simple and complex syntax strings -- 312int(7) 313int(7) 314 315Notice: Undefined variable: needleS in %s on line %d 316 317Warning: strpos(): Empty needle in %s on line %d 318bool(false) 319int(7) 320int(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 => int(0) 327 328*** Testing error conditions *** 329Warning: strpos(): Empty needle in %s on line %d 330bool(false) 331 332Warning: strpos() expects at least 2 parameters, 0 given in %s on line %d 333NULL 334 335Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d 336NULL 337 338Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d 339NULL 340 341Warning: strpos() expects at most 3 parameters, 4 given in %s on line %d 342NULL 343 344Warning: strpos(): Offset not contained in string in %s on line %d 345bool(false) 346 347Warning: strpos(): Offset not contained in string in %s on line %d 348bool(false) 349 350Warning: strpos(): Empty needle in %s on line %d 351bool(false) 352 353Done 354