Home php教程 php手册 php字符串分割函数explode的实例代码

php字符串分割函数explode的实例代码

Jun 06, 2016 pm 08:06 PM
explode String splitting

在php中分割一个字符串,我们可以使用函数explode(),其原型如下

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

该函数有3个参数,第一个参数$separator设置一个分割字符(串)。第二个参数$string指定所要操作的字符串。$limit参数是可选的,指定最多将字符串分割为多少个子串。
该函数返回一个由被分割的子串组成的数组。

来看下面的例子,对一个由逗号分隔的多行文本数据进行分析。
例1,分割字符串。

 代码如下:

$this_year = 2013;
$text = <<< EOT
祝无双,F,1982,广东,普遍职员
李三兵,M,1981,河北,普通职员
赵朴秀,F,1980,韩国,项目经理
EOT;
$lines = explode("\n", $text); //将多行数据分开
foreach ($lines as $userinfo) {
$info = explode(",", $userinfo, 3); //仅分割前三个数据
$name = $info[0];
$sex = ($info[1] == "F")? "女" : "男";
$age = $this_year - $info[2];
echo "姓名: $name $sex . 年龄:$age
";
}
/* 输出结果是:
姓名:祝无双 女 年龄:31
姓名:李三兵 男 年龄:32
姓名:赵朴秀 女 年龄:33
*/
?>

以上代码,先对文本按行进行分割,然后将每行字符串按","进行分割,并取前三个数据进行处理分析,然后进行整理并输出。

另外,为大家介绍php的另一个内建函数implode(),用于连接数组成为字符串。

与分割字符串函数相对应的是implode()函数,它的别名函数叫做join(),函数原型分别如下。
string implode(string $glue, array $pieces)
string join(string $glue, array $pieces)

implode()或join()函数可以将数组$pieces中的元素用指定的字符$glue连接起来。
下面为大家举一个简单的例子,供学习参考。

例2:

 代码如下:

$fruits = array('apple', 'banana', 'pear');
$str = implode(", ", $fruits);
echo $str;
?>

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 admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

How to use the split() function in Python 3.x to split a string according to the specified delimiter How to use the split() function in Python 3.x to split a string according to the specified delimiter Jul 31, 2023 pm 08:33 PM

Python is a popular programming language that provides many built-in functions to handle strings. One of the commonly used functions is the split() function, which can split a string into multiple substrings according to the specified delimiter. This article will introduce how to use the split() function in Python3.x. In Python, the split() function is a built-in function of the string class. Its basic syntax is as follows: string.split(separator,maxsplit)

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

Split string into array using PHP function 'explode' Split string into array using PHP function 'explode' Jul 24, 2023 pm 11:09 PM

Use the PHP function "explode" to split a string into an array. In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples. The basic syntax of the "explode" function is as follows: arrayexplode(

What should I do if php explode reports an error? What should I do if php explode reports an error? Jan 18, 2023 am 10:13 AM

Solution to php explode error: 1. Find and open the erroneous PHP code; 2. Find the explode function part; 3. Modify the code to "dump(explode(',',$str));die;", that is, use Just comma-separate the array.

How to use explode function to split string in PHP How to use explode function to split string in PHP Jun 26, 2023 pm 12:03 PM

In the PHP language, there are many basic functions that can help us process strings quickly and efficiently. Among them, the explode function is a very practical string splitting function. It can split a string into arrays according to the specified delimiter, and then perform more flexible string operations. In this article, we will introduce how to use the explode function to split strings in PHP. 1. The format of the explode function. The format of the explode function in the PHP language is as follows: explode(separa

The maximum split length of a string such that each character in the string appears in a substring The maximum split length of a string such that each character in the string appears in a substring Aug 25, 2023 pm 02:41 PM

In this article, we will explore the problem of how to find the length of the maximizing partition of a string with unique characters. We first understand the problem statement and then study naive and efficient methods to solve this problem, including their respective algorithms and time complexities. Finally, we will implement the solution in C++. Problem Statement Given a string, split the string into as many substrings as possible so that each character in the string appears in only one substring. Returns the length of these maximizing splits. Naive approach The naive approach is to iterate through the string, recording the last occurrence of each character. Then, iterate over the string again and create a partition when the last occurrence of the current character is found. Algorithm (naive) to initialize an array to store strings in

See all articles