suchen
Heimphp教程php手册php explode函数实例代码

php explode函数实例代码

Jun 13, 2016 pm 12:01 PM
explodephp代码函数分割字符串实例Bündeln数组语法

explode() 函数把字符串分割为数组。
语法
explode(separator,string,limit)

参数 描述
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 未使用分割函数
可能是你设置的分割字符不存在
Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Herunterladen der Mac-Version des Atom-Editors

Herunterladen der Mac-Version des Atom-Editors

Der beliebteste Open-Source-Editor

VSCode Windows 64-Bit-Download

VSCode Windows 64-Bit-Download

Ein kostenloser und leistungsstarker IDE-Editor von Microsoft

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

SublimeText3 Englische Version

SublimeText3 Englische Version

Empfohlen: Win-Version, unterstützt Code-Eingabeaufforderungen!

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor