Php string splitting function explode instance code _ PHP Tutorial

Source: Internet
Author: User
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...

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.