1--TEST-- 2Test mb_convert_encoding() function : usage variations - pass different data types as $to_encoding arg 3--SKIPIF-- 4<?php 5extension_loaded('mbstring') or die('skip'); 6function_exists('mb_convert_encoding') or die("skip mb_convert_encoding() is not available in this build"); 7?> 8--FILE-- 9<?php 10/* Prototype : string mb_convert_encoding(string $str, string $to_encoding [, mixed $from_encoding]) 11 * Description: Returns converted string in desired encoding 12 * Source code: ext/mbstring/mbstring.c 13 */ 14 15 16/* 17 * Pass different data types to $to_encoding arg to test behaviour of mb_convert_encoding 18 */ 19 20echo "*** Testing mb_convert_encoding() : usage variations ***\n"; 21 22// Initialise function arguments not being substituted 23mb_internal_encoding('utf-8'); 24$sourcestring = b'hello, 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 "Class A object"; 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 $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 behaviour of mb_convert_encoding() 95$iterator = 1; 96foreach($inputs as $input) { 97 echo "\n-- Iteration $iterator --\n"; 98 var_dump(bin2hex( mb_convert_encoding($sourcestring, $input, 'ISO-8859-1') )); 99 $iterator++; 100}; 101 102fclose($fp); 103 104echo "Done"; 105?> 106--EXPECTF-- 107*** Testing mb_convert_encoding() : usage variations *** 108 109-- Iteration 1 -- 110 111Warning: mb_convert_encoding(): Unknown encoding "0" in %s on line %d 112string(0) "" 113 114-- Iteration 2 -- 115 116Warning: mb_convert_encoding(): Unknown encoding "1" in %s on line %d 117string(0) "" 118 119-- Iteration 3 -- 120 121Warning: mb_convert_encoding(): Unknown encoding "12345" in %s on line %d 122string(0) "" 123 124-- Iteration 4 -- 125 126Warning: mb_convert_encoding(): Unknown encoding "-2345" in %s on line %d 127string(0) "" 128 129-- Iteration 5 -- 130 131Warning: mb_convert_encoding(): Unknown encoding "10.5" in %s on line %d 132string(0) "" 133 134-- Iteration 6 -- 135 136Warning: mb_convert_encoding(): Unknown encoding "-10.5" in %s on line %d 137string(0) "" 138 139-- Iteration 7 -- 140 141Warning: mb_convert_encoding(): Unknown encoding "123456789000" in %s on line %d 142string(0) "" 143 144-- Iteration 8 -- 145 146Warning: mb_convert_encoding(): Unknown encoding "1.23456789E-9" in %s on line %d 147string(0) "" 148 149-- Iteration 9 -- 150 151Warning: mb_convert_encoding(): Unknown encoding "0.5" in %s on line %d 152string(0) "" 153 154-- Iteration 10 -- 155string(24) "68656c6c6f2c20776f726c64" 156 157-- Iteration 11 -- 158string(24) "68656c6c6f2c20776f726c64" 159 160-- Iteration 12 -- 161 162Warning: mb_convert_encoding(): Unknown encoding "1" in %s on line %d 163string(0) "" 164 165-- Iteration 13 -- 166string(24) "68656c6c6f2c20776f726c64" 167 168-- Iteration 14 -- 169 170Warning: mb_convert_encoding(): Unknown encoding "1" in %s on line %d 171string(0) "" 172 173-- Iteration 15 -- 174string(24) "68656c6c6f2c20776f726c64" 175 176-- Iteration 16 -- 177string(24) "68656c6c6f2c20776f726c64" 178 179-- Iteration 17 -- 180string(24) "68656c6c6f2c20776f726c64" 181 182-- Iteration 18 -- 183string(24) "68656c6c6f2c20776f726c64" 184 185-- Iteration 19 -- 186string(24) "68656c6c6f2c20776f726c64" 187 188-- Iteration 20 -- 189string(24) "68656c6c6f2c20776f726c64" 190 191-- Iteration 21 -- 192 193Warning: mb_convert_encoding(): Unknown encoding "Class A object" in %s on line %d 194string(0) "" 195 196-- Iteration 22 -- 197string(24) "68656c6c6f2c20776f726c64" 198 199-- Iteration 23 -- 200string(24) "68656c6c6f2c20776f726c64" 201 202-- Iteration 24 -- 203 204Warning: mb_convert_encoding() expects parameter 2 to be string, resource given in %s on line %d 205string(0) "" 206Done 207 208