Home >Backend Development >PHP Problem >What is the method to split string in php

What is the method to split string in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-28 10:55:5638782browse

What is the method to split string in php

PHP string splitting

is used to split strings.

The related functions are as follows:

·explode(): Use one string to split another string.

·str_split(): Split the string into an array.

explode()

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

Syntax:

array explode( string separator, string string [, int limit] )

Parameter description:

What is the method to split string in php

Related recommendations: "PHP Getting Started Tutorial"

Example:

<?php
$str = &#39;one|two|three|four&#39;;
print_r(explode(&#39;|&#39;, $str));
print_r(explode(&#39;|&#39;, $str, 2));
// 负数的 limit(自 PHP 5.1 起)
print_r(explode(&#39;|&#39;, $str, -1));
?>

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
)

str_split()

str_split() Split the string into an array, successful Return an array.

Syntax:

array str_split( string string [, int length] )

Parameter description:

What is the method to split string in php

Example:

<?php
$str = &#39;one two three&#39;;
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>

The output result 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
)

The above is the detailed content of What is the method to split string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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