Php string splitting function explode instance code

Source: Internet
Author: User

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;
?>

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.