1--TEST-- 2Test mb_substr_count() function :usage variations - pass different data types as $encoding arg 3--SKIPIF-- 4<?php 5extension_loaded('mbstring') or die('skip'); 6function_exists('mb_substr_count') or die("skip mb_substr_count() is not available in this build"); 7?> 8--FILE-- 9<?php 10/* Prototype : int mb_substr_count(string $haystack, string $needle [, string $encoding]) 11 * Description: Count the number of substring occurrences 12 * Source code: ext/mbstring/mbstring.c 13 */ 14 15/* 16 * Pass different data types as $encoding argument to mb_substr_count() to test behaviour 17 * Where possible, 'UTF-8' is entered as string value 18 */ 19 20echo "*** Testing mb_substr_count() : usage variations ***\n"; 21 22// Initialise function arguments not being substituted (if any) 23$haystack = 'hello, world'; 24$needle = 'world'; 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 "UTF-8"; 35 } 36} 37 38// heredoc string 39$heredoc = <<<EOT 40UTF-8 41EOT; 42 43// get a resource variable 44$fp = fopen(__FILE__, "r"); 45 46// unexpected values to be passed to $encoding 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*/ "UTF-8", 78 'UTF-8', 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_substr_count() 95$iterator = 1; 96foreach($inputs as $input) { 97 echo "\n-- Iteration $iterator --\n"; 98 var_dump( mb_substr_count($haystack, $needle, $input) ); 99 $iterator++; 100}; 101 102fclose($fp); 103 104echo "Done"; 105?> 106--EXPECTF-- 107*** Testing mb_substr_count() : usage variations *** 108 109-- Iteration 1 -- 110 111Warning: mb_substr_count(): Unknown encoding "0" in %s on line %d 112bool(false) 113 114-- Iteration 2 -- 115 116Warning: mb_substr_count(): Unknown encoding "1" in %s on line %d 117bool(false) 118 119-- Iteration 3 -- 120 121Warning: mb_substr_count(): Unknown encoding "12345" in %s on line %d 122bool(false) 123 124-- Iteration 4 -- 125 126Warning: mb_substr_count(): Unknown encoding "-2345" in %s on line %d 127bool(false) 128 129-- Iteration 5 -- 130 131Warning: mb_substr_count(): Unknown encoding "10.5" in %s on line %d 132bool(false) 133 134-- Iteration 6 -- 135 136Warning: mb_substr_count(): Unknown encoding "-10.5" in %s on line %d 137bool(false) 138 139-- Iteration 7 -- 140 141Warning: mb_substr_count(): Unknown encoding "123456789000" in %s on line %d 142bool(false) 143 144-- Iteration 8 -- 145 146Warning: mb_substr_count(): Unknown encoding "1.23456789E-9" in %s on line %d 147bool(false) 148 149-- Iteration 9 -- 150 151Warning: mb_substr_count(): Unknown encoding "0.5" in %s on line %d 152bool(false) 153 154-- Iteration 10 -- 155 156Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 157bool(false) 158 159-- Iteration 11 -- 160 161Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 162bool(false) 163 164-- Iteration 12 -- 165 166Warning: mb_substr_count(): Unknown encoding "1" in %s on line %d 167bool(false) 168 169-- Iteration 13 -- 170 171Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 172bool(false) 173 174-- Iteration 14 -- 175 176Warning: mb_substr_count(): Unknown encoding "1" in %s on line %d 177bool(false) 178 179-- Iteration 15 -- 180 181Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 182bool(false) 183 184-- Iteration 16 -- 185 186Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 187bool(false) 188 189-- Iteration 17 -- 190 191Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 192bool(false) 193 194-- Iteration 18 -- 195int(1) 196 197-- Iteration 19 -- 198int(1) 199 200-- Iteration 20 -- 201int(1) 202 203-- Iteration 21 -- 204int(1) 205 206-- Iteration 22 -- 207 208Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 209bool(false) 210 211-- Iteration 23 -- 212 213Warning: mb_substr_count(): Unknown encoding "" in %s on line %d 214bool(false) 215 216-- Iteration 24 -- 217 218Warning: mb_substr_count() expects parameter 3 to be string, resource given in %s on line %d 219NULL 220Done 221