Tuesday, October 7, 2025
HomeLanguagesPHP str_split() Function

PHP str_split() Function

The str_split() is an inbuilt function in PHP and is used to convert the given string into an array. This function basically splits the given string into smaller strings of length specified by the user and stores them in an array and returns the array.

Syntax:

array str_split($org_string, $splitting_length)

Parameters:
The function accepts two parameters and are described below:

  1. $org_string (mandatory): This refers to the original string that the user needs to split into an array.
  2. $splitting_length (optional): This refers to the length of each array element, we wish to split our string into. By default the function accepts the value as 1.

Return Values: The function returns an array. If the length parameter exceeds the length of the original string, then the whole string is returned as a single element. If the length parameter is less than 1, then False is returned. By default length is equal to 1.

Examples:

Input: "Hello"
Output:
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

The below program will explain the working of the str_split() function.




<?php
// PHP program to display the working of str_split()
  
$string = "Geeks";
  
// Since second argument is not passed,
// string is split into substrings of size 1.
print_r(str_split($string));
  
$string = "neveropen";
  
// Splits string into substrings of size 4
// and returns array of substrings.
print_r(str_split($string, 4))
?>


Output:

Array
(
    [0] => G
    [1] => e
    [2] => e
    [3] => k
    [4] => s
)
Array
(
    [0] => Geek
    [1] => sfor
    [2] => Geek
    [3] => s
)

Reference:
http://php.net/manual/en/function.str-split.php

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS