Explode ()

Source: Internet
Author: User

To split a string into an array

Explode() function IntroductionExplode() function to split a string into arrays. Syntax:Explode(Separator,string,limit). Separator, required. Specifies where to split the string. string, required. The string to split. Limit, optional. Specifies the maximum number of array elements that are returned. 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() will returnFALSE。 If the value contained in the separator isstringcannot be found, thenExplode() returns a containingstringan array of individual elements in the. If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will containstringthe remainder of the section. If the limit parameter is a negative number, it returns the exception of the last-limit all elements outside of an element. This feature is PHP 5.1.0New in the. ProgramList:Explode() Example01<?PHP02//Example 103$fruit= "Apple Banana Orange Lemon Mango Pear";04$fruitArray=Explode(" ",$fruit);05Echo $fruitArray[0];//Apple06Echo $fruitArray[1];//Banana07//Example 208$data= "Gonn:*:nowamagic:1000::/home/foo:/bin/sh";09List($user,$pass,$uid,$gid,$gecos,$home,$shell) =Explode(":",$data);10Echo $user;//Gonn11Echo $pass;// *12?>program Run Result:1Apple2Banana3GonnThe ProgramList: Explode () example using the limit parameter1<?PHP2$str= ' One|two|three|four ';3//Positive Limit4Print_r(Explode(' | ',$str, 2));5//negative limit (since PHP 5.1)6Print_r(Explode(' | ',$str,-1));7?>program Run Result:01Array02(03    [0] = = One04    [1] = two|three| Four05)06Array07(08    [0] = = One09    [1] = = Both10    [2] = =three11) ProgramList: Converts a string to an array of key values01<?PHP02//converts pure string into a trimmed keyed array03functionString2keyedarray ($string,$delimiter= ', ',$kv= ' = ') {04if($a=Explode($delimiter,$string)) {//Create parts05foreach($a  as $s) {// each part06if($s) {07if($pos=Strpos($s,$kv)) {//Key/value delimiter08$ka[Trim(substr($s, 0,$pos))] =Trim(substr($s,$pos+strlen($kv)));09        } Else{//key delimiter not found10$ka[] =Trim($s);11        }12      }13    }14return $ka;15  }16} //String2keyedarray17$string= ' a=>1, b=>23, $a, c=>45%, True, D=>ab C ';18Print_r(String2keyedarray ($string));19?>program Run Result: View Sourceprint? 1Array2(3[A]= 14[b]= 235    [0] = =$a6[C]= 45%7    [1] = =true8[d]=AB C9)

Explode ()

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.