-
$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
- */
- ?>
-
Copy Code
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:
- $fruits = Array (' Apple ', ' banana ', ' pear ');
- $str = Implode (",", $fruits);
- Echo $str;
- ?>
Copy Code
you may be interested in the article:php space split text Array example PHP split Chinese string array simple example PHP split in English string several methods PHP split () string segmentation Function Example PHP string segmentation function explode
|