Home  >  Article  >  Backend Development  >  php中explode与split的区别介绍_php技巧

php中explode与split的区别介绍_php技巧

WBOY
WBOYOriginal
2016-05-17 09:08:391002browse

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

函数原型: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