0

在 php 中不可能执行以下任一操作:

end(explode(',', $theString));
explode(',', $theString)[0];  // 5.4 i think you can?

但是还有其他方法吗?

我完全理解它只有 1 行额外的代码来获取值,但是如果在代码中的某个位置有一些这种情况,它就会变得有点混乱。

4

3 回答 3

1
list($target) = explode(",",$theString);

You can even apply this to arbitrary indices, not just the first:

list(,$second) = explode(",",$theString);
list($first,,$third) = explode(",",$theString);

And so on.

于 2013-10-15T21:53:28.707 回答
0

在 PHP 5.4 之前,只需一行即可:

<?php

list($f) = explode(',', $s);

// if you want to confuse people
($a = explode(',', $s)) && ($f = $a[0]);

?>
于 2013-10-15T21:55:38.857 回答
0

strrpos和怎么样substr

$value = substr($theString, strrpos($theString, ',')+1);
于 2013-10-15T21:57:28.650 回答