English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP str_split() Function Usage and Example

PHP String Character String Functions Manual

The str_split() function is used to split strings and convert them to arrays

Syntax

array str_split ( string $string [, int $split_length = 1 ] )

Definition and Usage

Used to convert string splitting to an array

Return Value

If the optional split_length parameter is specified, each element of the array is a character block of length split_length, otherwise each character block is a single character.
If split_length is less than 1If the split_length parameter exceeds the length of the string, the entire string will be returned as the only element of the array.

Parameter

Serial NumberParameter and Description
1

string

Input String

2

length

The length of each segment.

Online Example

Try the following example, split the string, note the difference between specifying the length and not specifying the length:

<?php
   //Split the string without specifying the split length
   3codebox.com"));
   //Split the string, specify the split length of2
   3codebox.com\2));
?>
Test and see‹/›

Output Result

Array
(
    [0] => n
    [1] => h
    [2] => o
    [3] => o
    [4] => o
    [5] => .
    [6] => c
    [7] => o
    [8] => m
)
Array
(
    [0] => nh
    [1] => oo
    [2] => o.
    [3] => co
    [4] => m
)

PHP String Character String Functions Manual