Explode and get a value in one line of code

Viewed 10061

Can you write the following in one line of code?

$foo = explode(":", $foo);
$foo = $foo[0];
4 Answers

Just completing @GSto answer, in case it helps:
I often have to deal with strings that can have 0 or more separators (colon in this example).

Here is a one-liner to handle such strings:

$first = stristr($foo,":") ? stristr($foo,":",true) : $foo;
Related