Open In App

PHP mb_str_split() Function

Last Updated : 19 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The mb_str_split() function was introduced in the release of PHP version 7.4.0 and it is only supported with PHP versions equal to or greater than 7.4.0. The mb_str_split() function serves as an alternate of str_split() function. It is used to split the given string with the specified length of chunks and returns an array on success and FALSE on failure but in PHP 8, it does not return FALSE on failure .

Syntax:

array mb_str_split(string $string, int $length, string $encoding)

Parameters:

Name

Type

Description

$string string The string which is to be splitted into chunks and it is required.
$length int Length of the substring in which the string is being split. It is optional parameter.
$encoding string Encoding format which is to be applied on the substring. It is an optional parameter and default value is null.

  

Example 1: In the below example, the word “Awesome” is being split by using mb_str_split() function and since this function returns an array of characters print_r() has been used to print the output.

PHP




<?php
 
print_r(mb_str_split("Awesome"));
?>


Output:

Array
(
    [0] => A
    [1] => w
    [2] => e
    [3] => s
    [4] => o
    [5] => m
    [6] => e
)

Example 2: In the below example, two variables $sentence and $word has been created. The $sentance is used to store any random sentence of string type whereas $word is used to store the array returned by mb_str_split(). The  basic idea of the code is to separate “GeeksforGeeks” from the sentence which is stored in $sentence. Here, mb_str_split() is used to separate the substring with the specified length and the array is stored in $word and the result is displayed accordingly.

PHP




<?php
 
$sentence = "GeeksforGeeks is Awesome";
 
$word = mb_str_split($sentence,13);
echo $word[0];
 
?>


Output:

GeeksforGeeks

Reference: https://www.php.net/manual/en/function.mb-str-split.php


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads