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