PHP string Segmentation Function Explode example code _php tutorial

Source: Internet
Author: User
Array explode (string $separator, string $string [, int $limit])

The function has 3 parameters, 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.
The function returns an array of separated substrings.

Take a look at the following example to analyze a comma-delimited multiline text data.
Example 1, splitting a string.

Copy the Code code as follows:
$this _year = 2013;
$text = <<< EOT
Best wishes, f,1982, Guangdong, General Staff
Lie triple, m,1981, Hebei, General Staff
Shoro, f,1980, South Korea, project manager
EOT;
$lines = explode ("\ n", $text); Separating multiple rows of data
foreach ($lines as $userinfo) {
$info = Explode (",", $userinfo, 3); Split only the first three data
$name = $info [0];
$sex = ($info [1] = = "F")? "Female": "Male";
$age = $this _year-$info [2];
echo "Name: $name $sex. Age: $age
";
}
/* The output results are:
Name: Best wishes for the age of the girl: 31
Name: Li Sanbing male Age: 32
Name: Jacques Show girl Age: 33
*/
?>

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

In addition, we introduce another built-in function of PHP implode (), which is used to concatenate arrays into strings.

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 connects the elements in the array $pieces with the specified character $glue.
Here is a simple example for you to study for reference.

Example 2:
Copy the Code code as follows:
$fruits = Array (' Apple ', ' banana ', ' pear ');
$str = Implode (",", $fruits);
Echo $str;
?>

http://www.bkjia.com/PHPjc/326642.html www.bkjia.com true http://www.bkjia.com/PHPjc/326642.html techarticle array Explode (string $separator, string $string [, int $limit]) The function has 3 parameters, the first parameter $separator sets a split character (string). The second parameter $string specifies what to do ...

  • 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.