PHP Explode function Instance Code _php Foundation

Source: Internet
Author: User
Tags arrays explode php explode
The explode () function splits the string into arrays.


Syntax


explode (separator,string,limit)


Parameters Description
Separator Necessary. Specify where to split the string.
String Necessary. The string to split.
Limit Optional. Specify the maximum number of array elements to return.



Description


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





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





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





If the limit argument is a negative number, all elements except the last-limit element are returned. This feature is new in the PHP 5.1.0.


Tips and Comments


Note: The parameter limit is added in the PHP 4.0.1.





Note: Although implode () can receive two parameter orders for historical reasons, explode () is not available. You must ensure that the separator parameter is not preceded by a string parameter.


Example


in this case, we'll split the string into arrays:


Copy Code code as follows:

<?php
$STR = "Hello World". It ' s a beautiful day. ";
Print_r (Explode ("", $str));
?>



Output:





Array


(


[0] =&gt; Hello


[1] =&gt; world.


[2] =&gt; It ' s


[3] =&gt; a


[4] =&gt; beautiful


[5] =&gt; day.


)





Explode Function Example Tutorial


Explode (string separator, string string [, int limit])


separator is an empty string ("") and Explode () returns FALSE.


If separator contains a value that is not found in string, explode () returns an array that contains a single element of string.


Copy Code code 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 when we process the date or get the file extension, the following example


Copy Code code as follows:

$file = "Www.jb51.net.gif";
$extArray = Explode ('. ', $file);
$ext = End ($extArray);
Echo $ext;
/*
The output value is. gif



to use some of the functions that appear with the error prompts


Note:separator cannot is an empty string. Note: The separator can not be an empty string.


the string to be split is empty





Definition and Usage do not use split functions


may be you set the split character does not exist

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.