php explode函数实例代码_php入门_脚本之家
explode() 函数把字符串分割为数组,多用于字符切割为数组,进而进行后续操作
explode() 函数把字符串分割为数组。
语法
explode(separator,string,limit)
说明
本函数返回由字符串组成的数组,其中的每个元素都是由 separator 作为边界点分割出来的子字符串。
separator 参数不能是空字符串。如果 separator 为空字符串(""),explode() 将返回 FALSE。如果 separator 所包含的值在 string 中找不到,那么 explode() 将返回包含 string 中单个元素的数组。
如果设置了 limit 参数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。
如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。此特性是 PHP 5.1.0 中新增的。
提示和注释
注释:参数 limit 是在 PHP 4.0.1 中加入的。
注释:由于历史原因,虽然 implode() 可以接收两种参数顺序,但是 explode() 不行。你必须保证 separator 参数在 string 参数之前才行。
例子
在本例中,我们将把字符串分割为数组:
代码如下:
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
输出:
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
explode函数实例教程
explode ( string separator, string string [, int limit] )
separator 为空字符串(""),explode() 将返回 FALSE。
如果 separator 所包含的值在 string 中找不到,那么 explode() 将返回包含 string 单个元素的数组。
代码如下:
//explode 实例一
$explode = "aaa,bbb,ccc,ddd,explode,jjjj";
$array = explode( ',' ,$explode );
print_r($array);
/*
结果为
Array
(
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
[4] => explode
[5] => jjjj
)
*/
//我们在处理日期或取得文件扩展名时就可以用explode函数与end函数操作,下面来看实例
代码如下:
$file ="www.jb51.net.gif";
$extArray = explode( '.' ,$file );
$ext = end($extArray);
echo $ext;
/*
输出值为.gif
使用些函数出现的错误提示有
Note: Separator cannot be an empty string. 注意:分割符不可以是空字符串。
要分割的字符串为空
Definition and Usage 未使用分割函数
可能是你设置的分割字符不存在

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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.

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

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(

Use the PHP function "explode" to split a string into multiple substrings. In PHP programming, we often encounter situations where we need to split a string into multiple substrings. At this time, PHP provides a very convenient function "explode" that can help us easily implement this function. The syntax of the "explode" function is as follows: arrayexplode(string$delimiter,string$string[,in

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.

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

In this article, learn how to use the PHPExplode() function, which is a predefined built-in PHP function. The explode function is used to "Split a string into smaller contents. The explode function in PHP enables us to break a string into smaller contents by breaking. This delimiter is called a delimiter. Syntax explode(delimiter, string, Number of elements) Parameter explosion function accepts three parameters, two of which are mandatory and one is optional. Let us discuss these three parameters. Separator This character specifies the critical point or point at which the string will be split, i.e. Whenever this character is found in a string, it symbolizes the end of one element and the beginning of another element in the array. String input string
