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?
我知道“explode”会拆分字符串并将其转换为每次出现的数组。但是,如何在第一次出现时拆分并在第一次出现后保留所有内容?
Examples:
例子:
$split = explode('-', 'orange-yellow-red');
echo $split[1]; // output: "yellow"
^ I would like this to output: yellow-red
我希望这输出:黄红色
$split = explode('-', 'chocolate-vanilla-blueberry-red');
echo $split[1]; // output: "vanilla"
^ I would like this to output: vanilla-blueberry-red
我希望这输出:香草蓝莓红
5 个解决方案
#1
38
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
#2
7
Have a look at the third parameter of explode
:
看看爆炸的第三个参数:
$limit
$极限
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
如果设置了limit并且为正数,则返回的数组将包含最多限制元素,最后一个元素包含其余字符串。
If the limit parameter is negative, all components except the last -limit are returned.
如果limit参数为负,则返回除最后一个-limit之外的所有组件。
If the limit parameter is zero, then this is treated as 1.
如果limit参数为零,则将其视为1。
$a=explode('-','chocolate-vanilla-blueberry-red', 2);
echo $a[1]; // outputs vanilla-blueberry-red
#3
1
$split = explode('-', 'chocolate-vanilla-blueberry-red');
unset($split[0]);
echo implode('-', $split); # vanilla-blueberry-red
#4
0
A solution without a variable :
没有变量的解决方案:
strtok('orange-yellow-red', '-');
echo strtok(null);
#5
-1
a regular expression perhaps?
也许正则表达式?
[^-]+-
#1
38
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
#2
7
Have a look at the third parameter of explode
:
看看爆炸的第三个参数:
$limit
$极限
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
如果设置了limit并且为正数,则返回的数组将包含最多限制元素,最后一个元素包含其余字符串。
If the limit parameter is negative, all components except the last -limit are returned.
如果limit参数为负,则返回除最后一个-limit之外的所有组件。
If the limit parameter is zero, then this is treated as 1.
如果limit参数为零,则将其视为1。
$a=explode('-','chocolate-vanilla-blueberry-red', 2);
echo $a[1]; // outputs vanilla-blueberry-red
#3
1
$split = explode('-', 'chocolate-vanilla-blueberry-red');
unset($split[0]);
echo implode('-', $split); # vanilla-blueberry-red
#4
0
A solution without a variable :
没有变量的解决方案:
strtok('orange-yellow-red', '-');
echo strtok(null);
#5
-1
a regular expression perhaps?
也许正则表达式?
[^-]+-