PHP Explode function Instance Code _php tutorial

Source: Internet
Author: User
Tags php explode
The explode () function splits the string into arrays.
Grammar
Explode (Separator,string,limit)
Parameters Description
Separator Necessary. Specifies where to split the string.
String Necessary. The string to split.
Limit Optional. Specifies the maximum number of array elements that are returned.

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

The separator parameter cannot be an empty string. If separator is an empty string (""), Explode () returns FALSE. If separator contains a value that is not found in a string, explode () returns an array containing a single element in the string.

If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string.

If the limit parameter is a negative number, all elements except the last-limit element are returned. This feature is new in PHP 5.1.0.
Hints and Notes
Note: The parameter limit is added in PHP 4.0.1.

Note: For historical reasons, although implode () can receive two parameter sequences, explode () does not work. You must ensure that the separator parameter is not preceded by a string parameter.
Example
In this example, we will split the string into arrays:
Copy CodeThe 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 (""), and explode () will return FALSE.
If separator contains a value that is not found in a string, explode () returns an array containing a single element of string.
Copy CodeThe code is as follows:
Explode instance One
$explode = "AAA,BBB,CCC,DDD,EXPLODE,JJJJ";
$array = Explode (', ', $explode);
Print_r ($array);
/*
Result is
Array
(
[0] = AAA
[1] = BBB
[2] = = CCC
[3] = DDD
[4] = explode
[5] = JJJJ
)
*/

We can use the explode function with the end function to manipulate the date or to get the file name extension, and see the example
Copy CodeThe code is as follows:
$file = "Www.jb51.net.gif";
$extArray = Explode ('. ', $file);
$ext = End ($extArray);
Echo $ext;
/*
Output value is. gif

There are some errors that occur with the function
Note:separator cannot is an empty string. Note: The separator cannot be an empty string.
The string to be split is empty

Definition and usage do not use a split function
It's possible that the split character you set doesn't exist.

http://www.bkjia.com/PHPjc/325106.html www.bkjia.com true http://www.bkjia.com/PHPjc/325106.html techarticle the explode () function splits the string into arrays. Syntax explode (separator,string,limit) parameter description separator required. Specifies where to split the string. string is required. The word to be split ...

  • 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.