Usage of php string splitting function explode

怪我咯
Release: 2023-03-13 06:44:01
Original
2228 people have browsed it

To split a string in php, we can use the function explode(), whose prototype is as follows

array explode (string $separator, string $string [, int $limit])

This function has 3 parameters. The first parameter $separator sets a splitting character (string). The second parameter $string specifies the string to be operated on. The $limit parameter is optional and specifies the maximum number of substrings to split the string into.
This function returns an array consisting of split substrings.

Look at the following example to analyze a multi-line text data separated by commas.
Example 1, split string.

The code is as follows:

";
}
/* 输出结果是:
姓名:祝无双 女 年龄:31
姓名:李三兵 男 年龄:32
姓名:赵朴秀 女 年龄:33
*/
?>
Copy after login

The above code first splits the text by lines, then splits each line of string by ",", and Take the first three data for processing and analysis, then organize and output.

In addition, I will introduce to you another built-in function of PHPimplode(), which is used to connect arrays into strings.

Corresponding to the split string function is the implode() function. Its alias function is called join(). The function prototypes are as follows.
string implode(string $glue, array $pieces)
string join(string $glue, array $pieces)

The implode() or join() function can combine the elements in the array $pieces Connect with the specified character $glue.
Here is a simple example for your reference.

Example 2:

The code is as follows:

Copy after login


The above is the detailed content of Usage of php string splitting function explode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!