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 = b'+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 if ($a !== false) { 41 var_dump(bin2hex($a)); 42 } 43 else { 44 var_dump($a); 45 } 46 echo "--Multibyte String --\n"; 47 $b = mb_substr($string_mb, 1, $i, 'UTF-8'); 48 if (strlen($a) == mb_strlen($b, 'UTF-8')) { // should return same length 49 var_dump(bin2hex($b)); 50 } else { 51 echo "Difference in length of ASCII string and multibyte string\n"; 52 } 53 54} 55 56echo "Done"; 57?> 58--EXPECT-- 59*** Testing mb_substr() : usage variations *** 60 61**-- Offset is: -60 --** 62-- ASCII String -- 63string(0) "" 64--Multibyte String -- 65string(0) "" 66 67**-- Offset is: -50 --** 68-- ASCII String -- 69string(0) "" 70--Multibyte String -- 71string(0) "" 72 73**-- Offset is: -40 --** 74-- ASCII String -- 75string(0) "" 76--Multibyte String -- 77string(0) "" 78 79**-- Offset is: -30 --** 80-- ASCII String -- 81string(0) "" 82--Multibyte String -- 83string(0) "" 84 85**-- Offset is: -20 --** 86-- ASCII String -- 87string(0) "" 88--Multibyte String -- 89string(0) "" 90 91**-- Offset is: -10 --** 92-- ASCII String -- 93string(20) "497320616e20456e676c" 94--Multibyte String -- 95string(56) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e3808230" 96 97**-- Offset is: 0 --** 98-- ASCII String -- 99string(0) "" 100--Multibyte String -- 101string(0) "" 102 103**-- Offset is: 10 --** 104-- ASCII String -- 105string(20) "497320616e20456e676c" 106--Multibyte String -- 107string(56) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e3808230" 108 109**-- Offset is: 20 --** 110-- ASCII String -- 111string(40) "497320616e20456e676c69736820737472696e67" 112--Multibyte String -- 113string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 114 115**-- Offset is: 30 --** 116-- ASCII String -- 117string(40) "497320616e20456e676c69736820737472696e67" 118--Multibyte String -- 119string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 120 121**-- Offset is: 40 --** 122-- ASCII String -- 123string(40) "497320616e20456e676c69736820737472696e67" 124--Multibyte String -- 125string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 126 127**-- Offset is: 50 --** 128-- ASCII String -- 129string(40) "497320616e20456e676c69736820737472696e67" 130--Multibyte String -- 131string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 132 133**-- Offset is: 60 --** 134-- ASCII String -- 135string(40) "497320616e20456e676c69736820737472696e67" 136--Multibyte String -- 137string(100) "e69cace8aa9ee38386e382ade382b9e38388e381a7e38199e380823031323334efbc95efbc96efbc97efbc98efbc99e38082" 138Done