1--TEST-- 2Test mb_substr() function : usage variations - pass different integers to $length 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/* 11 * Test how mb_substr() behaves when passed a range of integers as $length argument 12 */ 13 14echo "*** Testing mb_substr() : usage variations ***\n"; 15 16mb_internal_encoding('UTF-8'); 17 18$string_ascii = '+Is an English string'; //21 chars 19 20//Japanese string, 21 characters 21$string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII='); 22 23/* 24 * Loop through integers as multiples of ten for $offset argument 25 * 60 is larger than *BYTE* count for $string_mb 26 */ 27for ($i = -60; $i <= 60; $i += 10) { 28 if (@$a || @$b) { 29 $a = null; 30 $b = null; 31 } 32 echo "\n**-- Offset is: $i --**\n"; 33 echo "-- ASCII String --\n"; 34 $a = mb_substr($string_ascii, 1, $i); 35 if ($a !== false) { 36 var_dump(bin2hex($a)); 37 } 38 else { 39 var_dump($a); 40 } 41 echo "--Multibyte String --\n"; 42 $b = mb_substr($string_mb, 1, $i, 'UTF-8'); 43 if (strlen($a) == mb_strlen($b, 'UTF-8')) { // should return same length 44 var_dump(bin2hex($b)); 45 } else { 46 echo "Difference in length of ASCII string and multibyte string\n"; 47 } 48 49} 50 51echo "Done"; 52?> 53--EXPECT-- 54*** Testing mb_substr() : usage variations *** 55 56**-- Offset is: -60 --** 57-- ASCII String -- 58string(0) "" 59--Multibyte String -- 60string(0) "" 61 62**-- Offset is: -50 --** 63-- ASCII String -- 64string(0) "" 65--Multibyte String -- 66string(0) "" 67 68**-- Offset is: -40 --** 69-- ASCII String -- 70string(0) "" 71--Multibyte String -- 72string(0) "" 73 74**-- Offset is: -30 --** 75-- ASCII String -- 76string(0) "" 77--Multibyte String -- 78string(0) "" 79 80**-- Offset is: -20 --** 81-- ASCII String -- 82string(0) "" 83--Multibyte String -- 84string(0) "" 85 86**-- Offset is: -10 --** 87-- ASCII String -- 88string(20) "497320616e20456e676c" 89--Multibyte String -- 90string(56) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e3808230" 91 92**-- Offset is: 0 --** 93-- ASCII String -- 94string(0) "" 95--Multibyte String -- 96string(0) "" 97 98**-- Offset is: 10 --** 99-- ASCII String -- 100string(20) "497320616e20456e676c" 101--Multibyte String -- 102string(56) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e3808230" 103 104**-- Offset is: 20 --** 105-- ASCII String -- 106string(40) "497320616e20456e676c69736820737472696e67" 107--Multibyte String -- 108string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 109 110**-- Offset is: 30 --** 111-- ASCII String -- 112string(40) "497320616e20456e676c69736820737472696e67" 113--Multibyte String -- 114string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 115 116**-- Offset is: 40 --** 117-- ASCII String -- 118string(40) "497320616e20456e676c69736820737472696e67" 119--Multibyte String -- 120string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 121 122**-- Offset is: 50 --** 123-- ASCII String -- 124string(40) "497320616e20456e676c69736820737472696e67" 125--Multibyte String -- 126string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 127 128**-- Offset is: 60 --** 129-- ASCII String -- 130string(40) "497320616e20456e676c69736820737472696e67" 131--Multibyte String -- 132string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 133Done 134