1--TEST-- 2Test mb_strpos() function : usage variations - pass different data types as $offset arg 3--SKIPIF-- 4<?php 5extension_loaded('mbstring') or die('skip'); 6function_exists('mb_strpos') or die("skip mb_strpos() is not available in this build"); 7?> 8--FILE-- 9<?php 10/* Prototype : int mb_strpos(string $haystack, string $needle [, int $offset [, string $encoding]]) 11 * Description: Find position of first occurrence of a string within another 12 * Source code: ext/mbstring/mbstring.c 13 */ 14 15/* 16 * Pass mb_strpos different data types as $offset arg to test behaviour 17 */ 18 19echo "*** Testing mb_strpos() : usage variations ***\n"; 20 21// Initialise function arguments not being substituted 22$needle = b'a'; 23$haystack = b'string_val'; 24$encoding = 'utf-8'; 25 26//get an unset variable 27$unset_var = 10; 28unset ($unset_var); 29 30// get a class 31class classA 32{ 33 public function __toString() { 34 return "Class A object"; 35 } 36} 37 38// heredoc string 39$heredoc = <<<EOT 40hello world 41EOT; 42 43// get a resource variable 44$fp = fopen(__FILE__, "r"); 45 46// unexpected values to be passed to $offest argument 47$inputs = array( 48 49 // int data 50/*1*/ 0, 51 1, 52 12345, 53 -2345, 54 55 // float data 56/*5*/ 10.5, 57 -10.5, 58 12.3456789000e10, 59 12.3456789000E-10, 60 .5, 61 62 // null data 63/*10*/ NULL, 64 null, 65 66 // boolean data 67/*12*/ true, 68 false, 69 TRUE, 70 FALSE, 71 72 // empty data 73/*16*/ "", 74 '', 75 76 // string data 77/*18*/ "string", 78 'string', 79 $heredoc, 80 81 // object data 82/*21*/ new classA(), 83 84 // undefined data 85/*22*/ @$undefined_var, 86 87 // unset data 88/*23*/ @$unset_var, 89 90 // resource variable 91/*24*/ $fp 92); 93 94// loop through each element of $inputs to check the behavior of mb_strpos() 95$iterator = 1; 96foreach($inputs as $input) { 97 echo "\n-- Iteration $iterator --\n"; 98 var_dump( mb_strpos($haystack, $needle, $input, $encoding)); 99 $iterator++; 100}; 101 102fclose($fp); 103 104echo "Done"; 105?> 106--EXPECTF-- 107*** Testing mb_strpos() : usage variations *** 108 109-- Iteration 1 -- 110int(8) 111 112-- Iteration 2 -- 113int(8) 114 115-- Iteration 3 -- 116 117Warning: mb_strpos(): Offset not contained in string in %s on line %d 118bool(false) 119 120-- Iteration 4 -- 121 122Warning: mb_strpos(): Offset not contained in string in %s on line %d 123bool(false) 124 125-- Iteration 5 -- 126bool(false) 127 128-- Iteration 6 -- 129 130Warning: mb_strpos(): Offset not contained in string in %s on line %d 131bool(false) 132 133-- Iteration 7 -- 134 135Warning: mb_strpos(): Offset not contained in string in %s on line %d 136bool(false) 137 138-- Iteration 8 -- 139int(8) 140 141-- Iteration 9 -- 142int(8) 143 144-- Iteration 10 -- 145int(8) 146 147-- Iteration 11 -- 148int(8) 149 150-- Iteration 12 -- 151int(8) 152 153-- Iteration 13 -- 154int(8) 155 156-- Iteration 14 -- 157int(8) 158 159-- Iteration 15 -- 160int(8) 161 162-- Iteration 16 -- 163 164Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d 165bool(false) 166 167-- Iteration 17 -- 168 169Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d 170bool(false) 171 172-- Iteration 18 -- 173 174Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d 175bool(false) 176 177-- Iteration 19 -- 178 179Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d 180bool(false) 181 182-- Iteration 20 -- 183 184Warning: mb_strpos() expects parameter 3 to be long, string given in %s on line %d 185bool(false) 186 187-- Iteration 21 -- 188 189Warning: mb_strpos() expects parameter 3 to be long, object given in %s on line %d 190bool(false) 191 192-- Iteration 22 -- 193int(8) 194 195-- Iteration 23 -- 196int(8) 197 198-- Iteration 24 -- 199 200Warning: mb_strpos() expects parameter 3 to be long, resource given in %s on line %d 201bool(false) 202Done 203