Home  >  Article  >  Backend Development  >  The difference between explode and split functions in php_PHP tutorial

The difference between explode and split functions in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:11768browse

On one level, there is no difference between the explode and split functions in PHP. They are used to separate characters into arrays. But if you look carefully, there is still a difference between explode and split. Let me introduce them to you.

First let’s look at the definitions of the two methods:

Function prototype: array split (string $pattern, string $string [, int $limit])

Function prototype: array explode ( string $separator, string $string [, int $limit])

At first glance, there is no difference. They seem to have the same functions. I made this mistake. Please note that the first parameters of the two functions are string $pattern and string separator. One is that $pattern is a regular string, and the other is that $separator is an ordinary string. Look at the code below:

Example 2. split() Example
The code is as follows
 代码如下 复制代码

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

echo $test;//output txt

换成:

$test1 = end(split('.','abc.txt'));

echo $test1;//no output

Copy code

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

echo $test;//output txt

Replaced with:

$test1 = end(split('.','abc.txt'));


echo $test1;//no output


The correct way to use split is to add the escape symbol

$test1 = end(split('.','abc.txt'));

echo $test1;//output txt


Analysis: "." symbol is a keyword of regular expression, so split is invalid, while explode is valid.

Parse dates that may be separated by slashes, dots, or dashes:
// The delimiter can be a slash, dot, or horizontal line
$date = "04/30/1973 "; echo "Month: $month; Day: $day; Year: $year
n "; ?>  To emulate similar @chars = split( ' ', $str) behavior in Perl, please refer to the example in the preg_split() function. Note that pattern is a regular expression. If the delimiting character you want to use is a special character in a regular expression, you must escape it first. If you think split() (or any other regex function) behaves strangely, please read the regex.7 file included in the regex/ subdirectory of the PHP distribution. The file is in manual page format
http://www.bkjia.com/PHPjc/632794.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632794.htmlTechArticleAt one level, there is no difference between the explode and split functions in PHP. They are both used to separate characters into arrays. , but if you look carefully there is still a difference between explode and split, let me explain...
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