Using PHP str_split to Split Strings into Character Arrays

This article explains how to use PHP's str_split function to split strings into character arrays, covering basic syntax, optional split length, and examples including simple splitting, fixed-length chunks, and handling multibyte UTF-8 characters.

php Courses
php Courses
php Courses
Using PHP str_split to Split Strings into Character Arrays

In PHP, you may need to split a string into an array of characters; the built‑in str_split function makes this straightforward. This article introduces the function, its syntax, parameters, and several practical examples.

Basic syntax :

array str_split ( string $string [, int $split_length = 1 ] )

The $string parameter is the input string to be split. The optional $split_length parameter specifies the length of each resulting element and defaults to 1.

Below are three illustrative examples.

Example 1: Basic usage

$string = "Hello, World!";
$arr = str_split($string);
print_r($arr);

Output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] => ,
    [6] => 
    [7] => W
    [8] => o
    [9] => r
    [10] => l
    [11] => d
    [12] => !
)

This splits "Hello, World!" into individual characters, each stored as an array element.

Example 2: Specifying element length

$string = "PHP is awesome!";
$arr = str_split($string, 3);
print_r($arr);

Output:

Array
(
    [0] => PHP
    [1] =>  is
    [2] => aw
    [3] => es
    [4] => om
    [5] => e!
)

Here $split_length is set to 3, so the original string is divided into chunks of three characters each.

Example 3: Handling multibyte characters

// Using a UTF‑8 encoded string
$string = "你好,世界!";
$arr = str_split($string, 3);
print_r($arr);

Output:

Array
(
    [0] => 你好,
    [1] => 世界!
)

Even though each Chinese character occupies three bytes in UTF‑8, str_split correctly splits the string when $split_length is set to 3. However, for reliable multibyte handling you may prefer mb_str_split or preg_split.

Conclusion

The article demonstrates the basic usage of PHP's str_split function for turning strings into character arrays, shows how to control element length, and highlights considerations when dealing with multibyte UTF‑8 strings, recommending alternative functions for those cases.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPArrayString Manipulationstr_split
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.