Home  >  Article  >  Backend Development  >  Analysis of the difference between explode and split in php

Analysis of the difference between explode and split in php

伊谢尔伦
伊谢尔伦Original
2016-12-01 11:03:471087browse

I encountered some problems when using split today. I still don’t have a deep understanding of the function, so I’ll close it up and make a mark

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, and 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 following code:

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

Replace with:

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

The correct way to use split is: add escape symbols

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

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


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