What is the function to split string in php

(*-*)浩
Release: 2023-02-25 20:24:01
Original
6844 people have browsed it

php functions to split strings include explode() and str_split()

What is the function to split string in php

##explode()

This function is the inverse function of implode(). It uses one string to split another string and returns an array.

Grammar:(Recommended learning:PHP video tutorial)

array explode( string separator, string string [, int limit] )
Copy after login

Example:

$str = 'one|two|three|four';
print_r(explode('|', $str));
print_r(explode('|', $str, 2));
// 负数的 limit(自 PHP 5.1 起)
print_r(explode('|', $str, -1));
?>
Copy after login

The output results are as follows:

Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
Copy after login

str_split()

str_split() splits the string into an array and returns an array successfully.

Syntax:

array str_split( string string [, int length] )
Copy after login

Example:

$str = 'one two three';
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
Copy after login

The output is as follows:

Array
(
[0] => o
[1] => n
[2] => e
[3] =>
[4] => t
[5] => w
[6] => o
[7] =>
[8] => t
[9] => h
[10] => r
[11] => e
[12] => e
)
Array
(
[0] => one
[1] => tw
[2] => o t
[3] => hre
[4] => e
)
Copy after login

The above is the detailed content of What is the function to split string in php. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!