Computer >> Computer tutorials >  >> Programming >> PHP

str_split() function in PHP


The str_split() function is used to convert a string to an array.

Syntax

str_split(str, len)

Parameters

  • str − The string to split

  • len − The length of each array element. The default is 1.

Return

The str_split() function returns FALSE, if the length is less than 1. If len is larger than the length of string, the entire string will be returned as the only element of the array.

Example

The following is an example −

<?php
   $str = "Android";
   print_r(str_split($str));
?>

Output

The following is the output −

Array
(
   [0] => A
   [1] => n
   [2] => d
   [3] => r
   [4] => o
   [5] => i
   [6] => d
)

Example

Let us see another example −

<?php
   $str = "DavidBeckham";
   print_r(str_split($str, 3))
?>

Output

The following is the output −

Array
(
   [0] => Dav
   [1] => idB
   [2] => eck
   [3] => ham
)