Home >Backend Development >PHP Tutorial >php explode php explode function example code

php explode php explode function example code

WBOY
WBOYOriginal
2016-07-29 08:48:061433browse

The explode() function splits a string into an array.
Syntax
explode(separator,string,limit)

Parameters Description
separator Required. Specifies where to split the string.
string Required. The string to split.
limit optional. Specifies the maximum number of array elements returned.

Explanation
This function returns an array composed of strings, each element of which is a substring separated by separator as a boundary point.
separator parameter cannot be an empty string. If separator is the empty string (""), explode() returns FALSE. If separator contains a value that is not found in string, explode() returns an array containing a single element from string.
If the limit parameter is set, the returned array contains at most limit elements, and the last element will contain the remainder of the string.
If the limit parameter is negative, all elements except the last -limit elements are returned. This feature is new in PHP 5.1.0.
Tips and Notes
Note: The parameter limit was added in PHP 4.0.1.
Note: Due to historical reasons, although implode() can receive two parameter orders, explode() cannot. You must ensure that the separator parameter comes before the string parameter.
Example
In this example, we will split the string into an array:

Copy code The code is as follows:


$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>


Output:
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
explode function example tutorial
explode ( string separator, string string [, int limit] )
separator is For an empty string (""), explode() will return FALSE.
If the separator contains a value that is not found in string, then explode() will return an array containing a single element of string.

Copy the code The code is as follows:


//explode Example 1
$explode = "aaa,bbb,ccc,ddd,explode,jjjj";
$array = explode( ',' ,$explode ) ;
print_r($array);
/*
The result is
Array
(
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
[4] => explode
[5] => jjjj
)
*/


//We can use the explode function and end function when processing dates or obtaining file extensions. Let’s look at examples below

Copy the code The code is as follows:


$file ="www.jb51.net.gif";
$extArray = explode( '.' ,$file );
$ext = end($extArray) ;
echo $ext;
/*
The output value is .gif


The error message that appears when using some functions is
Note: Separator cannot be an empty string. Note: The separator cannot be an empty string.
The string to be split is empty
Definition and Usage The split function is not used
It may be that the split character you set does not exist

The above introduces the example code of php explode php explode function, including the content of php explode. I hope it will be helpful to friends who are interested in PHP tutorials.

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