In the above we introduce the definition of function explode () in PHP, and we all know that the explode () function is a delimited string, so today we will introduce you to the PHP function explode () sub-instance!
To split a string in PHP, we can use the function explode (), which is prototyped as follows
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.
The code is as follows:
<?php$this_year = $text = <<< eot, f,1982, Guangdong, General Staff lie triple Soldier, m,1981, Hebei, General Staff Shoro, f,1980, Korea, project manager EoT; $lines Explode ("\ n", $text); Separate the multiline data into foreach ($lines as $userinfo) { $info = explode (",", $userinfo, 3); Only the first three data are divided $name = $info [0]; $sex = ($info [1] = = "F")? "Female": "Male"; $age = $this _year-$info [2]; echo "Name: $name $sex. Age: $age <br/> ";} /* The output is: Name: Best wishes for the age: 31 Name: Li Sanbing male Age: 32 Name: Jacques Show female 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:
The code is as follows:
<?php$fruits = Array (' Apple ', ' banana ', ' pear '); $str = Implode (",", $fruits); Echo $str; >
Summarize:
Through the study of this paper, the small partners do not think PHP explode () function is very simple to use, the details of a lot of boys are starting to try, then quickly use it!
Related recommendations:
A detailed definition of the explode () function in PHP
SUBSTR () function in PHP and explode () function Usage Example
php explode () function and implode () function usage instructions