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() 只返回非空的成分。

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools