Get the first value of a comma separated string

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?

+3
source share
4 answers

What about

echo reset(explode(',', 'a,b,c,d'))
+6
source
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

+6
source
<?php    
$array = explode(',', 'a,b,c,d');
$first = $array [0];
+4
source

Steve

A bit shorter

strtok('a,b,c,d', ",")
+3
source

All Articles