Search code examples
phparraysexplode

Explode and get a value in one line of code


Can you write the following in one line of code?

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

Solution

  • you could use stristr for this:

    $foo = stristr($foo,":",true);
    

    where true sets it to give you everything before the first instance of ":"