Monday, 29 September 2014

str_split in PHP

PHP str_split() function is utilized to split a string into an array.

Syntax:

str_split(string,length)
string : Required. Specifies the input string.

length : Optional.Specifies the length of each array element. Default is 1

Note: If length is less than 1, the PHP str_split() function will return FALSE.

Note:If length is greater than the length of string, the entire string will be returned as the only element of the array.

Example:

<?php
$str_name = "Good Morning";
print_r(str_split($str_name));
echo "<br /> <strong>Example using length parameter : </strong><br />";
print_r(str_split($str_name,3));
?>
Output will be:
Array ( 
        [0] => G 
        [1] => o
        [2] => o 
        [3] => d 
        [4] => 
        [5] => M 
        [6] => o 
        [7] => r
        [8] => n
        [9] => i 
        [10] => n 
        [11] => g 
       )
Example using length parameter : 
Array ( 
        [0] => Goo 
        [1] => d M 
        [2] => orn
        [3] => ing
       )

0 comments:

Post a Comment