search
Homephp教程php手册php字符串分割(explode str_split preg_split

分隔字符串,使用“str_split”就可以了好处是连空格也会作为数组的元素。我之前的例子就是因为前一个字符串包含2个空格,而后一个只有一个。但是输出的时候看到的显示都是一样的。 也可以按照其他分隔符进行分割,如“explode”或者“preg_split”,  

分隔字符串,使用“str_split”就可以了好处是连空格也会作为数组的元素。我之前的例子就是因为前一个字符串包含2个空格,而后一个只有一个。但是输出的时候看到的显示都是一样的。
也可以按照其他分隔符进行分割,如“explode”或者“preg_split”,


php教程 explode() 函数
php string 函数
定义和用法
explode() 函数把字符串分割为数组。

语法
explode(separator,string,limit)参数 描述
separator 必需。规定在哪里分割字符串。
string 必需。要分割的字符串。
limit 可选。规定所返回的数组元素的最大数目。

例子
在本例中,我们将把字符串分割为数组:

$str = "hello world. it's a beautiful day.";
print_r (explode(" ",$str));
?>

输出:

array
(
[0] => hello
[1] => world.
[2] => it's
[3] => a
[4] => beautiful
[5] => day.
)

str_split 拆分函数

定义和用法
str_split() 函数把字符串分割到数组中。

语法
str_split(string,length)参数 描述
string 必需。规定要分割的字符串。
length 可选。规定每个数组元素的长度。默认是 1。

说明
如果 length 小于 1,str_split() 函数将返回 false。

如果 length 大于字符串的长度,整个字符串将作为数组的唯一元素返回。
例子
例子 1

print_r(str_split("hello"));
?>

输出:

array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
)

例子 2

print_r(str_split("hello",3));
?>

输出:

array
(
[0] => hel
[1]


preg_split -- 用正则表达式分割字符串
说明
array preg_split ( string pattern, string subject [, int limit [, int flags]])


返回一个数组,包含 subject 中沿着与 pattern 匹配的边界所分割的子串。

如果指定了 limit,则最多返回 limit 个子串,如果 limit 是 -1,则意味着没有限制,可以用来继续指定可选参数 flags。

flags 可以是下列标记的任意组合(用按位或运算符 | 组合):


preg_split_no_empty
如果设定了本标记,则 preg_split() 只返回非空的成分。


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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),