1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00
Files
archived-php-src/ext/intl/tests/grapheme_str_split.phpt
Yuya Hamada 44e8301cf6 Add grapheme_str_split function
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
2024-04-10 18:20:23 +01:00

59 lines
1.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--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 nullbyte, length 1 [6e,75,6c,6c,00,62,79,74,65]