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

分隔字符串函数在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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool