PHP之implode与explode函数讲解
implode
(PHP 4, PHP 5)
implode — 将一个一维数组的值转化为字符串
说明¶
$glue , array $pieces )$pieces )用 glue 将一维数组的值连接为一个字符串。
Note:
因为历史原因implode()的参数顺序可以是随意的,explode()也一样
参数¶
glue-
默认为空的字符串。
pieces-
你想要转换的数组。
返回值¶
返回一个字符串,其内容为由 glue 分割开的数组的值。
更新日志¶
| 版本 | 说明 |
|---|---|
| 4.3.0 | glue 变为可选参数。 |
范例¶
Example #1 implode() 例子
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""
?>
注释¶
Note: 此函数可安全用于二进制对象。
explode
(PHP 4, PHP 5)
explode — 使用一个字符串分割另一个字符串
说明¶
$delimiter , string $string [, int $limit ] )此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。
参数¶
delimiter-
边界上的分隔字符。
string-
输入的字符串。
limit-
如果设置了
limit参数并且是正数,则返回的数组包含最多limit个元素,而最后那个元素将包含string的剩余部分。如果
limit参数是负数,则返回除了最后的 -limit个元素外的所有元素。如果
limit是 0,则会被当做 1。
由于历史原因,虽然 implode() 可以接收两种参数顺序,但是 explode() 不行。你必须保证 separator 参数在 string 参数之前才行。
返回值¶
此函数返回由字符串组成的 array,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。
如果 delimiter 为空字符串(""),explode() 将返回 FALSE。 如果 delimiter 所包含的值在 string 中找不到,并且使用了负数的 limit , 那么会返回空的 array, 否则返回包含 string 单个元素的数组。
更新日志¶
| 版本 | 说明 |
|---|---|
| 5.1.0 | 支持负数的 limit |
| 4.0.1 | 增加了参数 limit |
范例¶
Example #1 explode() 例子
<?php
// 示例 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[]; // piece1
echo $pieces[]; // piece2
// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
Example #2 explode() return examples
<?php
/* A string that doesn't contain the delimiter will simply return a one-length array of the original string. */
$input1 = "hello";
$input2 = "hello,there";
var_dump( explode( ',', $input1 ) );
var_dump( explode( ',', $input2 ) );
?>
以上例程会输出:
array(1)
(
[0] => string(5) "hello"
)
array(2)
(
[0] => string(5) "hello"
[1] => string(5) "there"
)
Example #3 limit 参数的例子
<?php
$str = 'one|two|three|four';
// 正数的 limit
print_r(explode('|', $str, ));
// 负数的 limit(自 PHP 5.1 起)
print_r(explode('|', $str, -));
?>
以上例程会输出:
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
注释¶
Note: 此函数可安全用于二进制对象。
参见¶
- preg_split() - 通过一个正则表达式分隔字符串
- str_split() - 将字符串转换为数组
- mb_split() - 使用正则表达式分割多字节字符串
- str_word_count() - 返回字符串中单词的使用情况
- strtok() - 标记分割字符串
- implode() - 将一个一维数组的值转化为字符串
PHP之implode与explode函数讲解的更多相关文章
- ***PHP implode() 函数,将数组合并为字符串;explode() 函数,把字符串打散为数组
实例 把数组元素组合为字符串: <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo implode(" ...
- ***实用函数:PHP explode()函数用法、切分字符串,作用,将字符串打散成数组
下面是根据explode()函数写的切分分割字符串的php函数,主要php按开始和结束截取中间数据,很实用 代码如下: <? // ### 切分字符串 #### function jb51net ...
- PHP中implode()和explode()
1, implode()函数返回由数组元素组合成的字符串,函数语法:string implode(separator,array),separator参数可选,规定数组元素之间放置的内容,默认是空字符 ...
- avascript中的this与函数讲解
徐某某 一个半路出家的野生程序员 javascript中的this与函数讲解 前言 javascript中没有块级作用域(es6以前),javascript中作用域分为函数作用域和全局作用域.并且,大 ...
- PHP explode()函数
源起:将日期格式的字符串拆分成年.月.日,用于组织关系介绍信的特定位置打印.感谢倪同学提供思路 定义和用法 explode()函数把字符串分割为数组 语法 explode(separator,stri ...
- split(),preg_split()与explode()函数分析与介
split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45 来源:尔玉毕业设计 评论:0 点击:965 split()函数可以实 ...
- Python中高阶函数讲解
高阶函数讲解 1. 常规高阶函数 递归函数 格式:def func_name(variable): '''__doc__'''#函数的说明文档 if 条件表达式:#限制递归退出值 pass retur ...
- python ---split()函数讲解
python ---split()函数讲解 split中文翻译为分裂. 在python用于分割字符串使用. split()就是将一个字符串分裂成多个字符串组成的列表. split()可以传入参数,也可 ...
- MySql UNIX_TIMESTAMP和FROM_UNIXTIME函数讲解
MySql UNIX_TIMESTAMP和FROM_UNIXTIME函数讲解 by:授客 QQ:1033553122 1. unix_timestamp(date)将时间转换为时间戳,如果参数为空,则 ...
随机推荐
- Row_Number实现分页(适用SQL)
1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号的集合 2:再查询该集合的 第 1 ...
- 对象this、currentTarget和target
在事件处理程序内部,对象this始终等于currentTarget的值,而target则只包含事件的实际目标.如果直接将事件处理程序指定给了目标元素,则this.currentTarget和targe ...
- BeanDefinition的Resource定位——2
1.FileSystemXmlApplicationContext的实现 public class FileSystemXmlApplicationContext extends AbstractXm ...
- springmvc 精华
Spring Mvc简介: Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求 ...
- 嵌入式系统图形库GUI核心模块介绍
本文转载自:http://blog.csdn.net/xteda/article/details/6575278 (作者 冯青华 信庭嵌入式工作室(www.xteda.com)- CEO Blog:h ...
- 使用记事本+CSC编译程序
本次学习主要阐明.NET开发可以不使用VS,也能像Java一样使用命令窗口.但实际意义不大. 诸位看管不必太留意.(仅供留着给公司新人,树(tree)新(new)风(bee)用.) 1.新建一个文本文 ...
- windows store app 读写图片
using System; using System.Threading.Tasks; using System.Runtime.InteropServices.WindowsRuntime; usi ...
- UINavigationController 与 UITabBarController
http://www.cnblogs.com/YouXianMing/p/3756904.html // index start from 1. UITabBarItem *newsItem = [[ ...
- nginx自启动脚本
#!/bin/bash # #Startup script for Nginx - this script starts and stops the nginx daemon # # chkconfi ...
- PHP 图片文件上传代码分享
分享下php上传图片文件的一段代码,挺不错的. 通过 PHP,可以把文件上传到服务器.加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 当然了,会禁止上传php文件,以及其它程序代 ...