这可以用于每10个字符拆分一个多字节字符串吗?
$string = 'Star Wars Episode Seven Sucked';
mb_split('.', $string, 10);
PHP手册说str_split()
适用于字节,而不适用于多字节字符串中的字符。这意味着mb_split()
似乎是一个自然的超载"替换,但这两个函数(str_split()
和mb_split()
)具有不同的函数签名,并且不是"重载伙伴,"可以这么说。然后,我想了想,这个怎么样?
mb_internal_encoding("UTF-8");
$string = 'Star Wars Episode Seven Sucked';
$tokens = [];
for($i = 0, $length = mb_strlen($string); $i < $length; $i += 10)
{
$tokens[] = mb_substr($string, $i, 10, 'UTF-8');
}
print_r($tokens);
答案 0 :(得分:-1)
function mb_utf8_split($string, $interval)
{
$tokens = [];
mb_internal_encoding('UTF-8');
$stringEncoding = mb_detect_encoding($string, 'UTF-8, ISO-8859-1', true);
if(!$stringEncoding)
{
throw new RuntimeException("Unable to identify character encoding.");
}
if($stringEncoding !== 'UTF-8')
{
$string = mb_convert_encoding($string, 'UTF-8', $stringEncoding);
}
for($i = 0, $length = mb_strlen($string); $i < $length; $i += $interval)
{
$tokens[] = mb_substr($string, $i, $interval, 'UTF-8');
}
return $tokens;
}