Can you write the following in one line of code?
$foo = explode(":", $foo);
$foo = $foo[0];
Can you write the following in one line of code?
$foo = explode(":", $foo);
$foo = $foo[0];
you could use stristr for this:
$foo = stristr($foo,":",true);
where true sets it to give you everything before the first instance of ":"
As an alternative to list(), you may use array_shift()
$foo = array_shift(explode(':', $foo));
Yes, it's posible to do using list
:
list($foo) = explode(":", $foo);
...[0]
code unlike Python or Perl. And that's why list
was added into language. –
Beatty 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;
© 2022 - 2024 — McMap. All rights reserved.