1--TEST-- 2grapheme_str_split function tests 3--EXTENSIONS-- 4intl 5--FILE-- 6<?php 7function ut_main() 8{ 9 $res_str = ''; 10 11 $tests = array( 12 array( "abc", 3, array("abc") ), 13 array( "abc", 2, array("ab", "c") ), 14 array( "abc", 1, array("a", "b", "c" ) ), 15 array( "土下座♀を", 1, array("土", "下", "座", "♀", "を") ), 16 array( "土下座♀を", 6, array("土下座♀を") ), 17 array( "null\x00byte", 1, array("n", "u", "l", "l", "\x00", "b", "y", "t", "e") ), 18 ); 19 20 foreach( $tests as $test ) { 21 $res_str .= "grapheme cluster for str_split - param {$test[0]}, length {$test[1]} "; 22 $result = grapheme_str_split($test[0], $test[1]); 23 $res_str .= dump_array_bin2hex($test[count($test)-1]) . check_result($result, $test[count($test)-1]) . "\n"; 24 } 25 return $res_str; 26} 27 28function dump_array_bin2hex($values) { 29 $returns = []; 30 foreach ($values as $value) { 31 $returns[] = bin2hex($value); 32 } 33 return '[' . implode(',', $returns) . ']'; 34} 35 36echo ut_main(); 37 38function check_result($result, $expected) { 39 40 if ( $result === false ) { 41 $result = 'false'; 42 } 43 44 if ( $result !== $expected) { 45 echo "result: {$result}\n"; 46 echo "expected: {$expected}\n"; 47 } 48 49 return ""; 50} 51?> 52--EXPECT-- 53grapheme cluster for str_split - param abc, length 3 [616263] 54grapheme cluster for str_split - param abc, length 2 [6162,63] 55grapheme cluster for str_split - param abc, length 1 [61,62,63] 56grapheme cluster for str_split - param 土下座♀を, length 1 [e59c9f,e4b88b,e5baa7,f09f9987e2808de29980,e38292] 57grapheme cluster for str_split - param 土下座♀を, length 6 [e59c9fe4b88be5baa7f09f9987e2808de29980e38292] 58grapheme cluster for str_split - param nullbyte, length 1 [6e,75,6c,6c,00,62,79,74,65] 59