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