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