Php string splitting function explode instance code. Arrayexplode (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 array explode (string $ separator, string $ string [, int $ limit]) to be operated.
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.
The code is as follows:
$ 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
";
}
/* 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:
The code is as follows:
$ Fruits = array ('apple', 'banana ', 'pear ');
$ Str = implode (",", $ fruits );
Echo $ str;
?>
Http://www.bkjia.com/PHPjc/326642.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326642.htmlTechArticlearray 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 operation...