1--TEST-- 2Test mb_substr() function : usage variations - Pass different data types as $encoding arg 3--SKIPIF-- 4<?php 5extension_loaded('mbstring') or die('skip'); 6function_exists('mb_substr') or die("skip mb_substr() is not available in this build"); 7?> 8--FILE-- 9<?php 10/* Prototype : string mb_substr(string $str, int $start [, int $length [, string $encoding]]) 11 * Description: Returns part of a string 12 * Source code: ext/mbstring/mbstring.c 13 */ 14 15/* 16 * Pass different data types as $encoding argument to mb_substr() to test behaviour 17 * Where possible, 'UTF-8' is entered as string value 18 */ 19 20echo "*** Testing mb_substr() : usage variations ***\n"; 21 22// Initialise function arguments not being substituted 23$str = 'string_val'; 24$start = 1; 25$length = 5; 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 "UTF-8"; 36 } 37} 38 39// heredoc string 40$heredoc = <<<EOT 41UTF-8 42EOT; 43 44// get a resource variable 45$fp = fopen(__FILE__, "r"); 46 47// unexpected values to be passed to $input argument 48$inputs = array( 49 50 // int data 51/*1*/ 0, 52 1, 53 12345, 54 -2345, 55 56 // float data 57/*5*/ 10.5, 58 -10.5, 59 12.3456789000e10, 60 12.3456789000E-10, 61 .5, 62 63 // null data 64/*10*/ NULL, 65 null, 66 67 // boolean data 68/*12*/ true, 69 false, 70 TRUE, 71 FALSE, 72 73 // empty data 74/*16*/ "", 75 '', 76 77 // string data 78/*18*/ "UTF-8", 79 'UTF-8', 80 $heredoc, 81 82 // object data 83/*21*/ new classA(), 84 85 // undefined data 86/*22*/ @$undefined_var, 87 88 // unset data 89/*23*/ @$unset_var, 90 91 // resource variable 92/*24*/ $fp 93); 94 95// loop through each element of $inputs to check the behavior of mb_substr 96$iterator = 1; 97foreach($inputs as $input) { 98 echo "\n-- Iteration $iterator --\n"; 99 $res = mb_substr($str, $start, $length, $input); 100 if ($res === false) { 101 var_dump($res); 102 } 103 else { 104 var_dump(bin2hex($res)); 105 } 106 $iterator++; 107}; 108 109fclose($fp); 110echo "Done"; 111?> 112--EXPECTF-- 113*** Testing mb_substr() : usage variations *** 114 115-- Iteration 1 -- 116 117Warning: mb_substr(): Unknown encoding "0" in %s on line %d 118bool(false) 119 120-- Iteration 2 -- 121 122Warning: mb_substr(): Unknown encoding "1" in %s on line %d 123bool(false) 124 125-- Iteration 3 -- 126 127Warning: mb_substr(): Unknown encoding "12345" in %s on line %d 128bool(false) 129 130-- Iteration 4 -- 131 132Warning: mb_substr(): Unknown encoding "-2345" in %s on line %d 133bool(false) 134 135-- Iteration 5 -- 136 137Warning: mb_substr(): Unknown encoding "10.5" in %s on line %d 138bool(false) 139 140-- Iteration 6 -- 141 142Warning: mb_substr(): Unknown encoding "-10.5" in %s on line %d 143bool(false) 144 145-- Iteration 7 -- 146 147Warning: mb_substr(): Unknown encoding "123456789000" in %s on line %d 148bool(false) 149 150-- Iteration 8 -- 151 152Warning: mb_substr(): Unknown encoding "1.23456789E-9" in %s on line %d 153bool(false) 154 155-- Iteration 9 -- 156 157Warning: mb_substr(): Unknown encoding "0.5" in %s on line %d 158bool(false) 159 160-- Iteration 10 -- 161 162Warning: mb_substr(): Unknown encoding "" in %s on line %d 163bool(false) 164 165-- Iteration 11 -- 166 167Warning: mb_substr(): Unknown encoding "" in %s on line %d 168bool(false) 169 170-- Iteration 12 -- 171 172Warning: mb_substr(): Unknown encoding "1" in %s on line %d 173bool(false) 174 175-- Iteration 13 -- 176 177Warning: mb_substr(): Unknown encoding "" in %s on line %d 178bool(false) 179 180-- Iteration 14 -- 181 182Warning: mb_substr(): Unknown encoding "1" in %s on line %d 183bool(false) 184 185-- Iteration 15 -- 186 187Warning: mb_substr(): Unknown encoding "" in %s on line %d 188bool(false) 189 190-- Iteration 16 -- 191 192Warning: mb_substr(): Unknown encoding "" in %s on line %d 193bool(false) 194 195-- Iteration 17 -- 196 197Warning: mb_substr(): Unknown encoding "" in %s on line %d 198bool(false) 199 200-- Iteration 18 -- 201string(10) "7472696e67" 202 203-- Iteration 19 -- 204string(10) "7472696e67" 205 206-- Iteration 20 -- 207string(10) "7472696e67" 208 209-- Iteration 21 -- 210string(10) "7472696e67" 211 212-- Iteration 22 -- 213 214Warning: mb_substr(): Unknown encoding "" in %s on line %d 215bool(false) 216 217-- Iteration 23 -- 218 219Warning: mb_substr(): Unknown encoding "" in %s on line %d 220bool(false) 221 222-- Iteration 24 -- 223 224Warning: mb_substr() expects parameter 4 to be string, resource given in %s on line %d 225string(0) "" 226Done 227