PHP 8.3.4 Released!

mb_str_split

(PHP 7 >= 7.4.0, PHP 8)

mb_str_splitGiven a multibyte string, return an array of its characters

Beschreibung

mb_str_split(string $string, int $length = 1, ?string $encoding = null): array

This function will return an array of strings, it is a version of str_split() with support for encodings of variable character size as well as fixed-size encodings of 1,2 or 4 byte characters. If the length parameter is specified, the string is broken down into chunks of the specified length in characters (not bytes). The encoding parameter can be optionally specified and it is good practice to do so.

Parameter-Liste

string

The String to split into characters or chunks.

length

If specified, each element of the returned array will be composed of multiple characters instead of a single character.

encoding

Der Parameter encoding legt die Zeichenkodierung fest. Wird er nicht übergeben, so wird die interne Zeichenkodierung genutzt.

A string specifying one of the supported encodings.

Rückgabewerte

mb_str_split() returns an array of strings.

Changelog

Version Beschreibung
8.0.0 encoding ist nun nullable (akzeptiert den null-Wert).
8.0.0 This function no longer returns false on failure.

Siehe auch

add a note

User Contributed Notes 3 notes

up
7
webmaster at redinfo dot co dot kr
1 year ago
if( !function_exists('mb_str_split')){
function mb_str_split( $string = '', $length = 1 , $encoding = null ){
if(!empty($string)){
$split = array();
$mb_strlen = mb_strlen($string,$encoding);
for($pi = 0; $pi < $mb_strlen; $pi += $length){
$substr = mb_substr($string, $pi,$length,$encoding);
if( !empty($substr)){
$split[] = $substr;
}
}
}
return $split;
}
}
up
7
info at ensostudio dot ru
3 years ago
Note: function return NULL if can't convert argument type.

Polyfill PHP < 7.4 based on package "symfony/polyfill-mbstring":
<?php
function mb_str_split($string, $split_length = 1, $encoding = null)
{
if (
null !== $string && !\is_scalar($string) && !(\is_object($string) && \method_exists($string, '__toString'))) {
trigger_error('mb_str_split(): expects parameter 1 to be string, '.\gettype($string).' given', E_USER_WARNING);
return
null;
}
if (
null !== $split_length && !\is_bool($split_length) && !\is_numeric($split_length)) {
trigger_error('mb_str_split(): expects parameter 2 to be int, '.\gettype($split_length).' given', E_USER_WARNING);
return
null;
}
$split_length = (int) $split_length;
if (
1 > $split_length) {
trigger_error('mb_str_split(): The length of each segment must be greater than zero', E_USER_WARNING);
return
false;
}
if (
null === $encoding) {
$encoding = mb_internal_encoding();
} else {
$encoding = (string) $encoding;
}

if (!
in_array($encoding, mb_list_encodings(), true)) {
static
$aliases;
if (
$aliases === null) {
$aliases = [];
foreach (
mb_list_encodings() as $encoding) {
$encoding_aliases = mb_encoding_aliases($encoding);
if (
$encoding_aliases) {
foreach (
$encoding_aliases as $alias) {
$aliases[] = $alias;
}
}
}
}
if (!
in_array($encoding, $aliases, true)) {
trigger_error('mb_str_split(): Unknown encoding "'.$encoding.'"', E_USER_WARNING);
return
null;
}
}

$result = [];
$length = mb_strlen($string, $encoding);
for (
$i = 0; $i < $length; $i += $split_length) {
$result[] = mb_substr($string, $i, $split_length, $encoding);
}
return
$result;
}
?>
up
-6
vovan-ve at yandex dot ru
3 years ago
Lazy polyfill for UTF-8 only:

function utf8_str_split(string $input, int $splitLength = 1)
{
$re = \sprintf('/\\G.{1,%d}+/us', $splitLength);
\preg_match_all($re, $input, $m);
return $m[0];
}
To Top