Array explode (string $ separator, string $ string [, int $ limit])
This function has three parameters. The first parameter $ separator sets a delimiter (string ). The second parameter $ string specifies the string to be operated. The $ limit parameter is optional and specifies the maximum number of substrings to be split.
This function returns an array composed of split substrings.
Let's take a look at the example below to analyze a multi-line text data separated by commas.
Example 1: Split the string.
Copy codeThe Code is as follows:
<? Php
$ This_year = 2013;
$ Text = <EOT
Zhu wushuang, F, 1982, Guangdong, General Staff
Li sanbing, M, 1981, Hebei, General Staff
Zhao pixiu, F, 1980, South Korea, Project Manager
EOT;
$ Lines = explode ("\ n", $ text); // separate multiple rows
Foreach ($ lines as $ userinfo ){
$ Info = explode (",", $ userinfo, 3); // only split the first three data items
$ Name = $ info [0];
$ Sex = ($ info [1] = "F ")? "Female": "male ";
$ Age = $ this_year-$ info [2];
Echo "name: $ name $ sex. age: $ age <br/> ";
}
/* The output result is:
Name: Zhu wushuang female age: 31
Name: Li sanbing male age: 32
Name: Zhao puxiu female age: 33
*/
?>
The above code first splits the text by line, then splits each line of strings by ",", and takes the first three data for processing and analysis, and then sorts and outputs them.
In addition, we will introduce another built-in function implode () in php, which is used to form a string of connections.
The implode () function corresponds to the split string function. Its alias function is join (). The function prototype is as follows.
String implode (string $ glue, array $ pieces)
String join (string $ glue, array $ pieces)
The implode () or join () function can connect elements in the array $ pieces with the specified character $ glue.
The following is a simple example for your reference.
Example 2:
Copy codeThe Code is as follows:
<? Php
$ Fruits = array ('apple', 'Banana ', 'pear ');
$ Str = implode (",", $ fruits );
Echo $ str;
?>