$ foo = explode(“","bla ble bli"))的快捷方式;回声$ foo [0] [英] Shortcut for: $foo = explode(" ", "bla ble bli"); echo $foo[0]
本文介绍了$ foo = explode(“","bla ble bli"))的快捷方式;回声$ foo [0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以在不使用变量的情况下获取分割后的字符串的第n个元素?
is there a way to get the n-th element of a splitted string without using a variable?
我的PHP代码始终如下所示:
My PHP code always looks like this:
$foo = explode(" ", "bla ble bli");
echo $foo[0];
是否有更短的方法,例如在Python中?
Is there a shorter way maybe like in Python?
print "bla ble bli".split(" ")[0]
谢谢.
推荐答案
在大多数情况下,这是人们应该使用的而不是explode
的方式:
This is what people should be using instead of explode
most of the time:
$foo = strtok("bla ble bli", " ");
它会截断第一个字符串部分,直到第一个" "
.
It cuts off the first string part until the first " "
.
如果不能放任自流,那么像Python中那样完成[0]
的最接近习惯用法是:
If you can't let go of explode, then the closest idiom to accomplish [0]
like in Python is:
$foo = current(explode(...));
如果不仅是第一个元素,那么它会变得有点麻烦:
If it's not just the first element, then it becomes a tad more cumbersome:
$foo = current(array_slice(explode(...), 2)); // element [2]
这篇关于$ foo = explode(“","bla ble bli"))的快捷方式;回声$ foo [0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文