PHP之string之explode()函数使用
explode
- (PHP 4, PHP 5, PHP 7)
- explode — Split a string by string
- explode — 使用一个字符串分割另一个字符串
Description
array explode (
    string $delimiter ,
    string $string [,
    int $limit = PHP_INT_MAX ]
    )
//Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
//此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。
Parameters
delimiter
- The boundary string.
- 边界上的分隔字符。
string
- The input string.
- 输入的字符串。
limit
- If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. 
- 如果设置了 limit 参数并且是正数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。 
- If the limit parameter is negative, all components except the last -limit are returned. 
- 如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。 
- If the limit parameter is zero, then this is treated as 1. 
- 如果 limit 是 0,则会被当做 1。 
Note:
- Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the delimiter argument comes before the string argument.
- 由于历史原因,虽然 implode() 可以接收两种参数顺序,但是 explode() 不行。你必须保证 separator 参数在 string 参数之前才行。
Return Values
- Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter. 
- 此函数返回由字符串组成的 array,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。 
- If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned. 
- 如果 delimiter 为空字符串(""),explode() 将返回 FALSE。 如果 delimiter 所包含的值在 string 中找不到,并且使用了负数的 limit , 那么会返回空的 array, 否则返回包含 string 单个元素的数组。 
Example
<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/2/16
 * Time: 下午3:49
 */
// 示例 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode( " ", $pizza );
echo $pieces[0] . PHP_EOL; // piece1
echo $pieces[1] . PHP_EOL; // piece2
// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list( $user, $pass, $uid, $gid, $gecos, $home, $shell ) = explode( ":", $data );
echo $user . PHP_EOL; // foo
echo $pass . PHP_EOL; // *
print_r( explode( ';', $data ) );//    [0] => foo:*:1023:1000::/home/foo:/bin/sh
$str = 'one|two|three|four';
// 正数的 limit
//[0] => one
//[1] => two|three|four
print_r( explode( '|', $str, 2 ) );
// 负数的 limit(自 PHP 5.1 起)
//[0] => one
//[1] => two
//[2] => three
//如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。
print_r( explode( '|', $str, - 1 ) );
$path = '/Users/zhangrongxiang/WorkSpace/phpProjects/PHPTEST';
//[0] =>
//[1] => Users
//[2] => zhangrongxiang
//[3] => WorkSpace
//[4] => phpProjects
//[5] => PHPTEST
$rs = explode( '/', $path );
print_r( $rs );
//[0] =>
//[1] => Users
//[2] => zhangrongxiang/WorkSpace/phpProjects/PHPTEST
$rs = explode( '/', $path, 3 );
print_r( $rs );
//[0] =>
//[1] => Users
//[2] => zhangrongxiang
$rs = explode( '/', $path, - 3 );
print_r( $rs );
/////////////////////////////////////////////////////////////////////////////////////
function multiexplode( $delimiters, $string ) {
	$ready = str_replace( $delimiters, $delimiters[0], $string );
	//here is a sample, this text, and this will be exploded, this also , this one too ,)
	echo $ready . PHP_EOL;
	$launch = explode( $delimiters[0], $ready );
	return $launch;
}
//[0] => here is a sample
//[1] =>  this text
//[2] =>  and this will be exploded
//[3] =>  this also
//[4] =>  this one too
//[5] => )
$text     = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode( array( ",", ".", "|", ":" ), $text );
print_r( $exploded );
/////////////////////////////////////////////////////////////////////////////////////
$str = "";
$res = explode( ",", $str );
//Array
//(
//	[0] =>
//)
print_r( $res );
$res = array_filter( explode( ",", $str ) );
//Array
//(
//)
print_r( $res );
/////////////////////////////////////////////////////////////////////////////////////
//a simple one line method to explode & trim whitespaces from the exploded elements
array_map( 'trim', explode( ",", $str ) );
$str = "one  ,two  ,       three  ,  four    ";
//[0] => one
//[1] => two
//[2] => three
//[3] => four
print_r( array_map( 'trim', explode( ",", $str ) ) );
/////////////////////////////////////////////////////////////////////////////////////
//the function
//Param 1 has to be an Array
//Param 2 has to be a String
function multiexplode2( $delimiters, $string ) {
	$ary = explode( $delimiters[0], $string );
	array_shift( $delimiters );
	if ( $delimiters != null ) {
		foreach ( $ary as $key => $val ) {
			$ary[ $key ] = multiexplode2( $delimiters, $val );
		}
	}
	return $ary;
}
// Example of use
$string     = "1-2-3|4-5|6:7-8-9-0|1,2:3-4|5";
$delimiters = Array( ",", ":", "|", "-" );
$res = multiexplode2( $delimiters, $string );
print_r( $res );
See
All rights reserved
PHP之string之explode()函数使用的更多相关文章
- PHP explode()函数
		源起:将日期格式的字符串拆分成年.月.日,用于组织关系介绍信的特定位置打印.感谢倪同学提供思路 定义和用法 explode()函数把字符串分割为数组 语法 explode(separator,stri ... 
- split(),preg_split()与explode()函数分析与介
		split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45 来源:尔玉毕业设计 评论:0 点击:965 split()函数可以实 ... 
- ***PHP implode() 函数,将数组合并为字符串;explode() 函数,把字符串打散为数组
		实例 把数组元素组合为字符串: <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo implode(" ... 
- ***实用函数:PHP explode()函数用法、切分字符串,作用,将字符串打散成数组
		下面是根据explode()函数写的切分分割字符串的php函数,主要php按开始和结束截取中间数据,很实用 代码如下: <? // ### 切分字符串 #### function jb51net ... 
- PHP之string之rtrim()函数使用
		rtrim (PHP 4, PHP 5, PHP 7) rtrim - Strip whitespace (or other characters) from the end of a string ... 
- PHP 中使用explode()函数切割字符串为数组
		explode()函数的作用:使用一个字符串分割另一个字符串,打散为数组. 例如: 字符串 $pizza = "第1 第2 第3 第4 第5 第6"; 根据空格分割后:$piece ... 
- php explode()函数 语法
		php explode()函数 语法 作用:把字符串打散为数组 语法:explode(separator,string,limit)大理石机械构件 参数: 参数 描述 separator 必需.规定在 ... 
- hive中的lateral view 与 explode函数的使用
		hive中的lateral view 与 explode函数的使用 背景介绍: explode与lateral view在关系型数据库中本身是不该出现的. 因为他的出现本身就是在操作不满足第一范式的数 ... 
- OC与c混编实现Java的String的hashcode()函数
		首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ... 
随机推荐
- 18-11-2 Scrum Meeting 5
			1. 会议照片 2. 工作记录 - 昨天完成工作 1 把数据导入数据库 2 中译英选择题和英译中选择题的查询接口 - 今日计划工作 1 配置页面 2 实现中译英选择题和英译中选择题的查询接口 3 整理 ... 
- Oracle ERP View - fnd_global.apps_initialize
			在ORACLE APPLICATION FORM中已存储了数据,在客户端TOAD中查找其TABLE找到相关数据行,但当查找其VIEW时就无法找到数据. 原因ORACLE的权责及OU安全机制屏蔽问题. ... 
- Android-自定义仿QQ列表Item滑动
			效果图: 布局中去指定自定义FrameLayout: <!-- 自定义仿QQ列表Item滑动 --> <view.custom.shangguigucustomview.MyCust ... 
- 浅议Github的注册和使用
			Self-introduction:编者本人叫司明周,现就读于南通大学计算机学院网络工程142班.爱好数学和音乐,喜欢数学中的逻辑性和天马行空的思维 编程能力:可以跳过略过得过且过吗..好吧,面对现实 ... 
- VMware虚拟机无法启动,提示“无法打开磁盘,未能锁定文件”
			VMware在进入linux时,提示:无法打开磁盘 F:\Debian\Debian linux2.6.x kernel.vmdk 或者某一个快照所依赖的磁盘原因: 未能锁定文件 问题出现的原因:虚拟 ... 
- Buffer Pool--锁定内存页
			锁定内存页在数据库中的优点和缺点: SQL Server 使用VirtualAlloc来分配内存,无内存压力时,SQL Server会尽可能地申请内存来缓存数据,当内存出现压力时,会出现缓存数据频繁地 ... 
- Solr相似度算法二:BM25Similarity
			BM25算法的全称是 Okapi BM25,是一种二元独立模型的扩展,也可以用来做搜索的相关度排序. Sphinx的默认相关性算法就是用的BM25.Lucene4.0之后也可以选择使用BM25算法(默 ... 
- windows服务安装记录
			首先打开cmd. 进入这个地址 C:\Windows\Microsoft.NET\Framework\v4.0.30319 执行操作 InstallUtil.exe E:\QueueWinServi ... 
- 数据与任务的并行---Parallel类
			Parallel类是对线程的抽象,提供数据与任务的并行性.类定义了静态方法For和ForEach,使用多个任务来完成多个作业.Parallel.For和Parallel.ForEach方法在每次迭代的 ... 
- NOIP2012BLOCKADE贪心思想证明
			NOIP2012BLOCKADE贪心思想证明 这道题的做法是二分时间并检验这个时间是否可行.检验的方法要用到贪心思想. 对于不能到根结点的军队应该尽量向根结点走. 如果军队A能走到根结点但到根结点后剩 ... 
