w3resource

PHP: str_split() function

PHP: Convert a string to an array

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:

<?php
$string_name='Welcome to w3resource.com';
print_r(str_split($string_name));
echo '<br>';
print_r(str_split($string_name,4));
echo '<br>';
?>

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  )

View the example in the browser

See also

PHP Function Reference

Previous: str_shuffle
Next: str_word_count



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/function-reference/str_split.php