Backend Development 5 min read

Using PHP str_split() Function: Syntax, Parameters, and Practical Examples

This article explains PHP's str_split() function, detailing its syntax, parameters, return values, and providing multiple code examples that demonstrate splitting strings into character arrays, handling custom split lengths, empty strings, and invalid lengths, while also noting multibyte character considerations.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP str_split() Function: Syntax, Parameters, and Practical Examples

PHP is a widely used server‑side scripting language designed for web development; it can be embedded in HTML or run via the command line. One of its powerful string‑handling functions is str_split() , which this article introduces in detail.

The str_split() function splits a string into an array of characters.

str_split() Function Syntax

<code>array str_split ( string $string [, int $split_length = 1 ] )</code>

Parameter Description:

$string : Required. The string to be split.

$split_length : Optional. Specifies the length of each chunk; defaults to 1.

The function returns an array where each element is a character from the original string. If $split_length is provided, each element will contain that many characters. Supplying a $split_length less than 1 causes the function to return false .

str_split() Function Usage Examples

1. Split string into character array:

<code>$str = "Hello World";
$arr = str_split($str);
print_r($arr);
</code>

Output:

<code>Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] => 
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)
</code>

2. Split string with specified length:

<code>$str = "Hello World";
$arr = str_split($str, 3);
print_r($arr);
</code>

Output:

<code>Array
(
    [0] => Hel
    [1] => lo
    [2] => Wor
    [3] => ld
)
</code>

3. Split an empty string:

<code>$str = "";
$arr = str_split($str);
print_r($arr);
</code>

Output:

<code>Array
(
)
</code>

4. Specify $split_length less than 1:

<code>$str = "Hello World";
$arr = str_split($str, 0);
var_dump($arr);
</code>

Output:

<code>bool(false)
</code>

The str_split() function is useful in many development scenarios, such as iterating over each character for manipulation or counting occurrences, and creating substrings of a specific length.

Note that str_split() works on single‑byte strings; when handling multibyte characters, the results may be incorrect. In such cases, consider using the mb_split() function.

Summary

The str_split() function is a practical PHP string utility that converts a string into an array of characters. Proper use of this function can simplify string operations and improve development efficiency.

PHP Learning Recommendations:

Vue3+Laravel8+Uniapp小白到实战开发教程

Vue3+TP6+API 社交电商系统开发教学

swoole从入门到精通推荐课程

《Workerman+TP6即时通讯聊天系统》限时秒杀!

BackendPHPcode examplesstring-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

login 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.