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