php中explode与split的区别介绍_PHP
首先来看下两个方法的定义:
函数原型: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有效。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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)".

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.

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.

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 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. -
