PHP Explode


Used to split a string to an array.

The correlation functions are as follows:


explode()

This function is the inverse of the implode() function, which uses a string to split another string and return an array.

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

parameter Description
separator Split flag
string The string to be split
limit Optional, indicating that the returned array contains up to limit elements, and the last element will contain the remainder of the string, supporting negative numbers.

example:

$str = 'one | two | three | four';
print_r(explode('|', $str));
print_r(explode('|', $str, 2));
// negative limit(since PHP 5.1)
print_r(explode('|', $str, -1));

The output is 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() splits the string into an array and returns an array.

grammar:

array str_split(string string [, int length])

Parameters Description
string The string to be split
length is optional, indicating that the length of each division unit, not less than 1

example:

$str = 'one two three';
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);

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
)



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook