PHP str split

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : str_split() function

Description

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

Version

(PHP 5 )

Syntax

str_split(string_name, split_length)

Parameters

Name

Description

Required /
Optional

Type

string_name

The string to be split.

Required

String

split_length

The length of the chunk.

Optional

Integer

Return value

If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise, each chunk will be one character in length.
FALSE is returned if split_length is less than 1.
If the split_length length exceeds the length of the string, the entire string is returned as the first (and only) array element.

Value Type : Array.

Pictorial Presentation

php-string-str_split()

Example :

  1. $string_name='Welcome to w3resource.com';  

  2. print_r(str_split($string_name));  

  3. echo '
    ';  

  4. print_r(str_split($string_name,4));  

  5. echo '
    ';  

  6. ?>   

Output :

Array ( [0] => W [1] => e [2] => l [3] => c [4] => o [5] => m [6] => e [7] => [8] => t [9] => o [10] => [11] => w [12] => 3 [13] => r [14] => e [15] => s [16] => o [17] => u [18] => r [19] => c [20] => e [21] => . [22] => c [23] => o [24] => m ) 
Array ( [0] => Welc [1] => ome [2] => to w [3] => 3res [4] => ourc [5] => e.co [6] => m )