PHP str_split() Function – Convert a String to an Array

The article explains PHP's str_split() function, which converts a string into an array of characters or fixed‑length chunks, details its parameters and return behavior, and provides example code with output, including demonstrating how split_length affects the result.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP str_split() Function – Convert a String to an Array

The PHP str_split() function converts a string into an array of substrings.

It accepts two parameters: string – the input string, and optional split_length – the length of each segment (default 1). If split_length is less than 1, the function returns FALSE; if it exceeds the string length, the entire string is returned as a single array element.

The function returns an array where each element contains a chunk of the original string, either a single character or a block of the specified length.

Example usage:

<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>

Output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)
Array
(
    [0] => Hel
    [1] => lo 
    [2] => Fri
    [3] => end
)
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.

BackendPHPStringArrayfunctionstr_split
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

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.