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/* 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 * Test how mb_substr() behaves when passed a range of integers as $length argument 17 */ 18 19echo "*** Testing mb_substr() : usage variations ***\n"; 20 21mb_internal_encoding('UTF-8'); 22 23$string_ascii = '+Is an English string'; //21 chars 24 25//Japanese string, 21 characters 26$string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII='); 27 28/* 29 * Loop through integers as multiples of ten for $offset argument 30 * 60 is larger than *BYTE* count for $string_mb 31 */ 32for ($i = -60; $i <= 60; $i += 10) { 33 if (@$a || @$b) { 34 $a = null; 35 $b = null; 36 } 37 echo "\n**-- Offset is: $i --**\n"; 38 echo "-- ASCII String --\n"; 39 $a = mb_substr($string_ascii, 1, $i); 40 var_dump(base64_encode($a)); 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(base64_encode($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(16) "SXMgYW4gRW5nbA==" 89--Multibyte String -- 90string(40) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMA==" 91 92**-- Offset is: 0 --** 93-- ASCII String -- 94string(0) "" 95--Multibyte String -- 96string(0) "" 97 98**-- Offset is: 10 --** 99-- ASCII String -- 100string(16) "SXMgYW4gRW5nbA==" 101--Multibyte String -- 102string(40) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMA==" 103 104**-- Offset is: 20 --** 105-- ASCII String -- 106string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc=" 107--Multibyte String -- 108string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=" 109 110**-- Offset is: 30 --** 111-- ASCII String -- 112string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc=" 113--Multibyte String -- 114string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=" 115 116**-- Offset is: 40 --** 117-- ASCII String -- 118string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc=" 119--Multibyte String -- 120string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=" 121 122**-- Offset is: 50 --** 123-- ASCII String -- 124string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc=" 125--Multibyte String -- 126string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=" 127 128**-- Offset is: 60 --** 129-- ASCII String -- 130string(28) "SXMgYW4gRW5nbGlzaCBzdHJpbmc=" 131--Multibyte String -- 132string(68) "5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=" 133Done 134