mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
I noticed that PHP does not have a grapheme cluster based str_split function. So I created the grapheme_str_split function. This feature will allow you to correctly handle emoji and variable selectors. Co-authored-by: Ayesh Karunaratne <Ayesh@users.noreply.github.com> Close GH-13580
59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
--TEST--
|
||
grapheme_str_split function tests
|
||
--EXTENSIONS--
|
||
intl
|
||
--FILE--
|
||
<?php
|
||
function ut_main()
|
||
{
|
||
$res_str = '';
|
||
|
||
$tests = array(
|
||
array( "abc", 3, array("abc") ),
|
||
array( "abc", 2, array("ab", "c") ),
|
||
array( "abc", 1, array("a", "b", "c" ) ),
|
||
array( "土下座🙇♀を", 1, array("土", "下", "座", "🙇♀", "を") ),
|
||
array( "土下座🙇♀を", 6, array("土下座🙇♀を") ),
|
||
array( "null\x00byte", 1, array("n", "u", "l", "l", "\x00", "b", "y", "t", "e") ),
|
||
);
|
||
|
||
foreach( $tests as $test ) {
|
||
$res_str .= "grapheme cluster for str_split - param {$test[0]}, length {$test[1]} ";
|
||
$result = grapheme_str_split($test[0], $test[1]);
|
||
$res_str .= dump_array_bin2hex($test[count($test)-1]) . check_result($result, $test[count($test)-1]) . "\n";
|
||
}
|
||
return $res_str;
|
||
}
|
||
|
||
function dump_array_bin2hex($values) {
|
||
$returns = [];
|
||
foreach ($values as $value) {
|
||
$returns[] = bin2hex($value);
|
||
}
|
||
return '[' . implode(',', $returns) . ']';
|
||
}
|
||
|
||
echo ut_main();
|
||
|
||
function check_result($result, $expected) {
|
||
|
||
if ( $result === false ) {
|
||
$result = 'false';
|
||
}
|
||
|
||
if ( $result !== $expected) {
|
||
echo "result: {$result}\n";
|
||
echo "expected: {$expected}\n";
|
||
}
|
||
|
||
return "";
|
||
}
|
||
?>
|
||
--EXPECT--
|
||
grapheme cluster for str_split - param abc, length 3 [616263]
|
||
grapheme cluster for str_split - param abc, length 2 [6162,63]
|
||
grapheme cluster for str_split - param abc, length 1 [61,62,63]
|
||
grapheme cluster for str_split - param 土下座🙇♀を, length 1 [e59c9f,e4b88b,e5baa7,f09f9987e2808de29980,e38292]
|
||
grapheme cluster for str_split - param 土下座🙇♀を, length 6 [e59c9fe4b88be5baa7f09f9987e2808de29980e38292]
|
||
grapheme cluster for str_split - param null |