Joey Morani
Joey Morani

Reputation: 26601

How to remove the first number in a string?

How can I remove the first number in a string? Say if I had these 48 numbers seperated with a ',' (comma):

8,5,8,10,15,20,27,25,60,31,25,39,25,31,26,28,80,28,27,31,27,29,26,35,8,5,8,10,15,20,27,25,60,31,25,39,25,31,26,28,80,28,27,31,27,29,26,35

How would I remove the "8," from the string? Thanks.

Upvotes: 4

Views: 2344

Answers (7)

Gordon
Gordon

Reputation: 317119

Removes all characters up to and including the first comma:

ltrim(strstr($numbers, ','), ',');

Removes all digits up to and including the first comma:

ltrim(ltrim($numbers, '01234567890'), ',');

Slightly faster version that removes all digits up to and including the first non-digit

substr($numbers, strspn($numbers, '1234567890')+1);

Upvotes: 2

Armand
Armand

Reputation: 24383

Like this works if '8' is bigger than 9!

$numbers = substr($numbers, strpos($text,",") + 1);

Upvotes: 0

nik
nik

Reputation: 3678

substr(strstr("8,5,8,10,15,20,27,25,60,31,25,39,25,31",","),1);

Upvotes: 0

soulmerge
soulmerge

Reputation: 75724

The following assumes you have at least 2 numbers (and is fast):

list($first, $rest) = explode(',', $string, 2);

This will work on single numbers, too (but uses regex):

preg_replace('/^.*?,/', '', $string);

Upvotes: 7

Denis Bazhenov
Denis Bazhenov

Reputation: 9955

substr(strchr("2512,12512,1245234,23452345", ","), 1)

actually. It the most efficient way I think because of it's not converting string into array or something. It;s just cut off string.

Upvotes: 8

Karthik
Karthik

Reputation: 3291

All are providing different solution, so here another simple solution

$mystring = "8,5,8,10,15,20,27,25,60,31,25,39,25,31";
$pos = strpos($mystring, ",");
$pos1 = $pos+1; // add the comma position by 1
echo substr($mystring, $pos1);

Upvotes: 2

Michał Mech
Michał Mech

Reputation: 2055

$text = "8,5,8,10,15,20,27,25,60,31,2";

First You can explode:

$array = explode(',', $text);

Then remove first element:

array_shift($array);

End at the last, join:

echo implode(',' $array);

Upvotes: 7

Related Questions