search
Homephp教程php手册php中explode与split的区别介绍

php中explode与split的区别介绍

Jun 13, 2016 am 11:57 AM
arrayexplodephpsplitandintroducefunctionthe differenceprototypedefinitionmethodof

首先来看下两个方法的定义:

函数原型:array split (string $pattern, string $string [, int $limit])

函数原型:array explode ( string $separator, string $string [, int $limit])

初看没有啥差别,貌似功能都一样。我就犯了这个错误。 请注意两个函数的第一个参数string $pattern和string separator,一个是$pattern说明是正则字符串,一个是$separator是普通字符串。

看下面的代码:

复制代码 代码如下:


$test = end(explode('.', 'abc.txt'));
echo $test;//output txt


换成:

复制代码 代码如下:


$test1 = end(split('.','abc.txt'));
echo $test1;//no output


用split的正确做法是:加转义符号

复制代码 代码如下:


$test1 = end(split('\.','abc.txt'));
echo $test1;//output txt



分析:"." 符号是正则表达式的关键字所以split无效,而explode有效。
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools