I am looking for the fastest / shortest way to get the first value from a string, separated by commas, in a string .
The best I can do is
$string = 'a,b,c,d'; echo "The first thing is " . end(array_reverse(explode(',', $string))) . ".";
but I feel that excessive and excessive. Is there a better way?
What about
echo reset(explode(',', 'a,b,c,d'))
list($first) = explode(',', 'a,b,c,d'); var_dump($first); // a
maybe it works :)
In PHP 6.0, you can simply:
$first = explode(',', 'a,b,c,d')[0];
But this is a syntax error in 5.x and below
<?php $array = explode(',', 'a,b,c,d'); $first = $array [0];
Steve
A bit shorter
strtok('a,b,c,d', ",")