$ foo = explode(“","bla ble bli"))的快捷方式;回声$ foo [0]

编程入门 行业动态 更新时间:2024-10-22 13:50:53
本文介绍了$ 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]

本文发布于:2023-11-17 01:58:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回声   快捷方式   quot   explode   foo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!