PHP.nl

str_split

str_split

Convert a string to an array

array **str_split** string $string int $length

Converts a string to an array.

stringThe input string.

lengthMaximum length of the chunk.

If the optional parameter is specified, the returned array will be broken down into chunks with each being in length, except the final chunk which may be shorter if the string does not divide evenly. The default is , meaning every chunk will be one byte in size. length``length``length``1

If is less than , a will be thrown. length``1``ValueError

**Voorbeeld: Example uses of **

<?php

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

?>
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Array
(
    [0] => Hel
    [1] => lo
    [2] => Fri
    [3] => end
)

Opmerking: > will split into bytes, rather than characters when dealing with a multi-byte encoded string. can be used to split the string into code points. can be used to split the string into grapheme clusters. str_split``mb_str_split``grapheme_str_split

mb_str_split``grapheme_str_split``chunk_split``preg_split``explode``count_chars``str_word_countfor