Introduction to the difference between explode and split in php

高洛峰
Release: 2023-03-02 19:14:01
Original
1605 people have browsed it

First, let’s look at the definitions of the next 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:
Copy code The code is as follows:
$test = end(explode('.', 'abc.txt'));
echo $test;//output txt

Replace with:
Copy code The code is as follows:
$test1 = end(split('.','abc.txt'));
echo $test1;//no output

The correct way to use split is to add escape symbols
Copy code The code is as follows :
$test1 = end(split('.','abc.txt'));
echo $test1;//output txt


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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!