Phpexplode function instance code _ PHP Tutorial

Source: Internet
Author: User
Phpexplode function instance code. The explode () function separates strings into arrays. The explode (separator, string, limit) parameter description is required. Specifies where to split the string. String is required. The explode () function separates strings into arrays.
Syntax
Explode (separator, string, limit)
Parameters Description
Separator Required. Specifies where to split the string.
String Required. The string to be split.
Limit Optional. Specifies the maximum number of returned array elements.

Description
This function returns an array composed of strings. each element is a substring separated by a separator as a boundary point.

The separator parameter cannot be a null string. If separator is a null string (""), explode () returns FALSE. If the value of separator cannot be found in string, explode () returns an array containing a single element in string.

If the limit parameter is set, the returned array contains up to limit elements, and the last element contains the rest of the string.

If the limit parameter is negative, all elements except the last-limit element are returned. This feature is added in PHP 5.1.0.
Tips and comments
Note: The limit parameter is added to PHP 4.0.1.

Note: For historical reasons, although implode () can receive two parameter sequences, explode () cannot. You must ensure that the separator parameter is prior to the string parameter.
Example
In this example, we split the string into an array:

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] =>
[4] => beautiful
[5] => day.
)

Explode function instance tutorial
Explode (string separator, string [, int limit])
Separator is a null string (""), and explode () returns FALSE.
If the value of separator cannot be found in string, explode () returns an array containing a single string element.

The code is as follows:


// Explode instance 1
$ Explode = "aaa, bbb, ccc, ddd, explode, jjjj ";
$ Array = explode (',', $ explode );
Print_r ($ array );
/*
Result:
Array
(
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
[4] => explode
[5] => JJJJJ
)
*/


// We can use the explode and end functions when processing the date or getting the file extension. the following shows the example.

The code is as follows:


$ File = "www.jb51.net.gif ";
$ ExtArray = explode ('.', $ file );
$ Ext = end ($ extArray );
Echo $ ext;
/*
Output value: .gif


Some function errors are prompted:
Note: Separator cannot be an empty string. Note: The delimiter cannot be a null string.
The string to be split is empty.

Definition and Usage no split function
It may be that the delimiter you set does not exist.

The http://www.bkjia.com/PHPjc/325106.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325106.htmlTechArticleexplode () function splits strings into arrays. The explode (separator, string, limit) parameter description is required. Specifies where to split the string. String is required. The word to be separated...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.