Home >php教程 >PHP源码 >php中explode高级分隔字符与分隔字符串

php中explode高级分隔字符与分隔字符串

WBOY
WBOYOriginal
2016-06-08 17:21:211023browse

分隔字符串函数在php中我们会用到explode与preg_split函数,但是小编用到最多的就是直接以一个字符分开了,如果有多个字符串我们好像就不知道如何使用此函数来分隔字符串了,下文来给各位介绍一下explode用法。

<script>ec(2);</script>

explode最简单用法

在本例中,我们将把字符串分割为数组:

 代码如下 复制代码

$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.
)

但如果碰到下面函数我们要如何来解决"祛痘、痘印#抗氧化:活肤滋润#对抗唇纹:提拉紧致#控油:保湿#改善唇色";

例子

 代码如下 复制代码

$string="祛痘、痘印#抗氧化:活肤滋润#对抗唇纹:提拉紧致#控油:保湿#改善唇色";   

$arr=explode('#',str_replace(array(":"),'#',$string));

或者使用preg_split方法达到相同的效果

$arr = preg_split( "/ (#|:) /", $string );

发现这样非常的好用了哦,多个不同规则我们都可以一条语句搞写哦。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn