如何在第一次出现某事时拆分一个字符串? [英] How can I split a string on the first occurrence of something?

查看:77
本文介绍了如何在第一次出现某事时拆分一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道"explode"会拆分字符串,并在每次出现时将其转换为数组.但是如何在第一次出现时拆分并在第一次出现后保留所有内容?

I know "explode" splits the string and turns it into an array for every occurrence. But how do I split on the first occurrence and keep everything after the first occurrence?

示例:

$split = explode('-', 'orange-yellow-red');
echo $split[1]; // output: "yellow"

^我想这样输出:黄色红色

$split = explode('-', 'chocolate-vanilla-blueberry-red');
echo $split[1]; // output: "vanilla"

^我想这样输出:香草蓝莓红色

推荐答案

您可以将limit用作

You can pass the limit as the third parameter of explode that will do the job.

$split = explode('-', 'orange-yellow-red',2);
echo $split[1]; //output yellow-red

这篇关于如何在第一次出现某事时拆分一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆