AppDividend
Latest Code Tutorials

PHP str_split Function Example | str_split() Function In PHP

0

PHP str_split Function Example | str_split() Function In PHP is today’s topic. The str_split() function splits a string into an array. The str_split() is the inbuilt function in PHP and is used to convert a given string into an array. The function splits the given string into smaller strings of the length specified by a user and stores them in the array and returns an array.

PHP str_split Function

The syntax of the str_split() method is following.

str_split(string, length)

The string parameter is required, and it specifies the string to split.

The length parameter is optional, and it specifies the length of each array element. Default is 1.

See the following code example.

<?php
print_r(str_split("Millie Bobby Brown", 6));

See the output.

➜  pro php app.php
Array
(
    [0] => Millie
    [1] =>  Bobby
    [2] =>  Brown
)
➜  pro

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.

The 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.

The str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string.

If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.

See another example.

<?php

print_r(str_split("KRUNAL"));

See the output.

➜  pro php app.php
Array
(
    [0] => K
    [1] => R
    [2] => U
    [3] => N
    [4] => A
    [5] => L
)
➜  pro

Finally, PHP str_split Function Example | str_split() Function In PHP is over.

Recommended Posts

PHP str_repeat() Function Example

PHP Functions Tutorial With Example

PHP chunk_split() Function Example Tutorial

PHP count_chars() Function Example Tutorial

PHP chop() Function Example

PHP money_format() Function Example Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.