当前位置: 首页 > php>阅读正文

php字符串分割expode函数

2021.9.12 朱丰华 1376 次 留下评论 503字

expode字符串分割,可以使用正则表达式

<?php
$str = "Hello world. I love Shanghai!";
print_r (explode(" ",$str));
?>

语法

explode(separator,string,limit)
参数描述
separator必需。规定在哪里分割字符串。
string必需。要分割的字符串。
limit可选。规定所返回的数组元素的数目。可能的值:大于 0 – 返回包含最多 limit 个元素的数组小于 0 – 返回包含除了最后的 –limit 个元素以外的所有元素的数组0 – 返回包含一个元素的数组

技术细节

返回值:返回字符串的数组
PHP 版本:4+
更新日志:在 PHP 4.0.1 中,新增了 limit 参数。在 PHP 5.1.0 中,新增了对负数 limit 的支持。
<?php
$str = 'one,two,three,four';

// 零 limit,表示所有
print_r(explode(',',$str,0));

// 正的 limit,表示起始
print_r(explode(',',$str,2));

// 负的 limit,表示截至
print_r(explode(',',$str,-1));
?>

本篇完,还有疑问?留下评论吧

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注