Home Backend Development PHP Tutorial php中explode与split的区别介绍_PHP

php中explode与split的区别介绍_PHP

Jun 01, 2016 pm 12:08 PM
explode split

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

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the split method in Java String How to use the split method in Java String May 02, 2023 am 09:37 AM

The split method in String uses the split() method of String to split the String according to the incoming characters or strings and return the split array. 1. General usage When using general characters, such as @ or, as separators: Stringaddress="Shanghai@Shanghai City@Minhang District@Wuzhong Road";String[]splitAddr=address.split("@");System .out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3

How to solve 'undefined: bytes.Split' error in golang? How to solve 'undefined: bytes.Split' error in golang? Jun 25, 2023 pm 02:02 PM

In the Go language, the bytes package is a package for manipulating byte types, and it contains many useful methods, such as the Split() method. However, when using the Split() method, you may encounter an "undefined: bytes.Split" error. This error is usually caused by incompatible Go versions or lack of necessary dependent libraries. This article will introduce some methods to solve this error. Method 1: Upgrade the Go version as follows

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

What is the use of split method in go language What is the use of split method in go language Jan 28, 2023 pm 01:37 PM

In the Go language, the Split() method is used to split a string. You can use delimiters to divide the string into a list of substrings, and the substrings are returned in the form of slices. Split() is a method of the strings package. You need to import the strings package before using it. The syntax is "strings.Split (string to be split, delimiter)".

How to use split in python How to use split in python Nov 17, 2023 am 10:13 AM

In Python, split() is a commonly used string method that splits a string into substrings and returns a list containing these substrings. This method can split a string into multiple parts based on the specified delimiter. The basic syntax is "str.split(separator, maxsplit)", str is the string to be split, separator is the separator, and maxsplit is an optional parameter, indicating the maximum number of splits.

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

Detailed explanation of split command in Linux Detailed explanation of split command in Linux Feb 21, 2024 pm 06:06 PM

Detailed explanation of split command in Linux split is a commonly used command in Linux. It is used to split a file into multiple smaller files. In this article, we will introduce the usage of split command in detail and provide some specific code examples. 1. Command syntax The basic syntax of the split command is as follows: split [options] [input file] [output file prefix] options: -: Split the file according to the specified number of lines, the default is 1000 lines. -

See all articles