Home > Backend Development > PHP Tutorial > php explode function example code_PHP tutorial

php explode function example code_PHP tutorial

WBOY
Release: 2016-07-21 15:20:24
Original
1222 people have browsed it

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

参数 描述
separator 必需。规定在哪里分割字符串。
string 必需。要分割的字符串。
limit 可选。规定所返回的数组元素的最大数目。

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 but 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 the 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 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 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 the example below
Copy 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 messages that appear when using some functions are
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325106.htmlTechArticleexplode() function splits a string into an array. Syntax explode(separator,string,limit) Parameter Description separator Required. Specifies where to split the string. string required. Words to split...
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