当前位置:Gxlcms > PHP教程 > php分割字符串的函数是什么

php分割字符串的函数是什么

时间:2021-07-01 10:21:17 帮助过:7人阅读

php分割字符串的函数有explode()和str_split()

explode()

本函数为 implode() 的反函数,使用一个字符串分割另一个字符串,返回一个数组。

语法: (推荐学习:PHP视频教程)

array explode( string separator, string string [, int limit] )

例子:

<?php
$str = 'one|two|three|four';
print_r(explode('|', $str));
print_r(explode('|', $str, 2));
// 负数的 limit(自 PHP 5.1 起)
print_r(explode('|', $str, -1));
?>

输出结果如下:

Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)

str_split()

str_split() 将字符串分割为一个数组,成功返回一个数组。

语法:

array str_split( string string [, int length] )

例子:

<?php
$str = 'one two three';
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>

输出结果如下:

Array
(
[0] => o
[1] => n
[2] => e
[3] =>
[4] => t
[5] => w
[6] => o
[7] =>
[8] => t
[9] => h
[10] => r
[11] => e
[12] => e
)
Array
(
[0] => one
[1] => tw
[2] => o t
[3] => hre
[4] => e
)

以上就是php分割字符串的函数是什么的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行