Home > Backend Development > PHP Tutorial > How to use php str_split function?

How to use php str_split function?

青灯夜游
Release: 2023-04-06 18:52:01
Original
3817 people have browsed it

php The str_split() function is used to split a given string into smaller string lengths specified by the user, store them in an array, and return the array; the syntax is "str_split(string ,length)", the parameter string specifies the string, the parameter length specifies the length of the array element, and the default value is 1.

How to use php str_split function?


php str_split() function

The php str_split() function splits the string into an array according to the specified length to form an array.

Syntax:

str_split(string,length)
Copy after login

Parameters:

● String: Required parameter, which means the user needs to split it into an array the original string.

●length: Optional parameter, specifying the length of each array element. By default, the accepted value is 1.

Return value: str_split() returns an array. If the length parameter exceeds the length of the original string, the entire string is returned as a single element. If the length parameter is less than 1, False is returned. By default, length is equal to 1.

Let’s take a look at how to use the php str_split() function through an example.

Example 1:

<?php
$i = "helloworld";
$j = str_split($i,3);//指定分割的数组元素长度为3
print_r($j); //输出数组用print_r函数
?>
Copy after login

Output:

Array ( [0] => hel [1] => low [2] => orl [3] => d )
Copy after login

Example 2:

<?php
$i = "you can study php in php.cn";
$j = str_split($i,5);
print_r($j);
?>
Copy after login

Output:

Array ( [0] => you c [1] => an st [2] => udy p [3] => hp in [4] => php. [5] => cn )
Copy after login

The above is the detailed content of How to use php str_split function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template