PHP explode()和implode()的使用方法

  在编程中经常使用到分割字符串为数组或将数组拼接成字符串的功能,大家应该会想到explode和implode这2个函数,我个人时常混淆explode和implode的作用,今天特地来详细学习这2个长得相似,功能相反的函数,免得日后再次混淆。

  为了学习这2个函数,特意下载了7.2版本的PHP手册,也为了日后方便深入学习PHP。


一、explode()

  先来了解explode,作为英语学渣之前一直读错,跟着有道词典读音反复读了很多遍才读对。

  下面来看手册函数介绍:

    explode 使用一个字符串分割另一个字符串

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

    此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 delimiter 作为边界点分割出来。

  看完手册内容我们大概知道这个explode函数是用一个分隔符分割字符串的函数,参数由3部分组成,分隔符、字符串、分隔限制多说无益,直接动手用几个例子来了解它把。

	$string = 'a,b,c,d,e,f';
	
	echo '<pre>';
	
	var_dump(explode(',', $string));
	
	var_dump(explode(',', $string,2));
	
	var_dump(explode(',', $string,-1));
	
	var_dump(explode(',', $string,0));
	
	var_dump(explode(',', $string,1));
	
	echo '</pre>';

运行结果:
在这里插入图片描述
  对于分割字符串来说,2个参数足以使用。当然也会有特殊情况,第三个参数通过正负来达到不同的功能,也是看了手册之后才知道explode还有这个功能。

  如果第三个参数是正数的话,代表分割时只会分割成几份,最后的一份会包含剩余部分。
  如果第三个参数是负数,则只分割除最后几项的的字符串。
  如果第三个参数是0,则和参数为1一样,返回整体字符串出来。


二、implode()

  下面来看下手册介绍:

    implode — 将一个一维数组的值转化为字符串

    string implode ( string $glue , array $pieces )

    string implode ( array $pieces )

    返回 用 一个连接符 将一维数组的值连接的一个字符串。

  下面来看几个例子:

	$array = ['a','b','c','d','e'];
	
	var_dump(implode('|',$array));
	
	echo "<br>";
	
	var_dump(implode($array));
	
	echo "<br>";
	
	var_dump(join('|',$array));
	
	echo "<br>";
	
	var_dump(join($array));

运行结果:

在这里插入图片描述
  可以看到如果不传入连接符的话,会将数组拆散直接连接成一个字符串。

  还可以看到join函数和implode函数功能一样,没错join是implode的一个别名。


三、总结

  explode函数 使用一个分割符将一个字符串分割成一个数组

  implode函数 使用一个分割符将一个数组拼接成一个字符串

  join函数 implode函数的别名

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The `implode()` function in PHP is used to join elements of an array into a string with a specified separator. It takes two parameters: the first parameter is the separator that will be used to join the array elements, and the second parameter is the array of elements to be joined. The function returns a string that is formed by joining the elements of the array with the specified separator. For example, if we have an array of strings like this: ``` $arr = array('apple', 'banana', 'pear'); ``` We can join the elements of the array into a string using the `implode()` function like this: ``` $str = implode(',', $arr); ``` This will create a string like this: ``` "apple,banana,pear" ``` The `explode()` function, on the other hand, is used to split a string into an array of substrings using a specified separator. It takes two parameters: the first parameter is the separator that will be used to split the string, and the second parameter is the string to be split. The function returns an array of substrings. For example, if we have a string like this: ``` $str = "apple,banana,pear"; ``` We can split the string into an array of substrings using the `explode()` function like this: ``` $arr = explode(',', $str); ``` This will create an array like this: ``` array('apple', 'banana', 'pear') ``` So, basically, the `implode()` function joins an array of elements into a string using a specified separator, while the `explode()` function splits a string into an array of substrings using a specified separator.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值