PHP string partition function Explode instance code _php skill

Source: Internet
Author: User
Tags explode

Array explode (string $separator, string $string [, int $limit])

The function has 3 parameters, and the first parameter $separator sets a split character (string). The second parameter $string specifies the string to manipulate. The $limit parameter is optional, specifying the maximum number of substrings to be split into.
This function returns an array of separated substrings.

Look at the example below to analyze a comma-delimited line of text data.
Example 1, split the string.

Copy Code code as follows:

<?php
$this _year = 2013;
$text = <<< EOT
Wish Matchless, f,1982, Guangdong, universal staff
Lie triple systems, m,1981, Hebei, General Staff
Shoro, f,1980, Korea, project manager
EOT;
$lines = explode ("\ n", $text); Separating multiple rows of data
foreach ($lines as $userinfo) {
$info = Explode (",", $userinfo, 3); Split the first three data only
$name = $info [0];
$sex = ($info [1] = = "F")? "Female": "Male";
$age = $this _year-$info [2];
echo "Name: $name $sex. Age: $age <br/> ";
}
/* Output results are:
Name: wish to be matchless female age: 31
Name: Li Sanbing male Age: 32
Name: Zhaopa female Age: 33
*/
?>

The above code, the text is divided by row, and then each line string by "," to split, and take the first three data processing analysis, and then collated and output.

In addition, introduce another PHP built-in function implode () for you to connect the array to become a string.

corresponding to the split string function is the implode () function, whose alias function is called join (), and the function prototypes are as follows.
String implode (string $glue, array $pieces)
String Join (string $glue, array $pieces)

The implode () or join () function can concatenate elements in an array $pieces with the specified character $glue.
Here is a simple example for you to learn from.

Example 2:

Copy Code code 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.