views:

224

answers:

8

Hello. 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.

A: 

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

$numbers = substr($numbers, strpos($text,",") + 1);
Alison
Too much `r` in the strpos ;)
Col. Shrapnel
8 is *never* bigger than 9 :confused:
soulmerge
Thanks, Colonel - will edit :-)
Alison
Good point, soulmerge - will edit :-)
Alison
+6  A: 
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.

dotsid
I did a quick test because I was curious -- this is indeed more efficient than @soulmerge's answer -- but only by about 90 bytes of memory and a few microseconds. In fact on some occasions the `explode` with a limit ran slightly faster. But in general the difference in speed was negligible.
Josh
The only drawback of this implementation is that it assumes the presence of a comma
soulmerge
@soulmerge it's also assuming there will be no other characters but numbers and the comma
Gordon
Yeah, but explode also assume that there is comma in string. Doesn't it? :)
dotsid
+7  A: 
$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);
Michał Mech
I will never understand why people use `explode` for simple string operations like this.
Gordon
It was my first thought. Last time I wrote something in PHP was more than year ago. I think that operations on strings are better solution.That's why I voted on one of the others answer ;)
Michał Mech
The only excuse is the OP probably needs an array, not a string actually.
Col. Shrapnel
Also, this is one of the few answers which actually removes the first number from the string and leaves both the number and the remainder of the string in tact. soulmerge's answer is more efficcient, however, if you're sure you only need one number. soulmerge's answer makes a fabulous case for explode.
Josh
+2  A: 

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);
Gordon
I was surprised to find this wasn't much faster than soulmerge's answer, and wasn't faster than dotsid's answer. Which is odd because I thought yours would be faster than both...
Josh
@Josh my Zend Debugger gives an average mean time of 0.000003 for @soulmerge's solution and also for @dotsid's and mine. There is a tiny difference when doing 10k repetitions. Without OPcode Caching I found @soulmerge's solution to run at half that speed, but even then, it's nothing to talk about. I didn't bench memory usage, which would be interesting to see for the explode examples, but I doubt it won't be neglectable. On a sidenote, my second code is the only one so far that removes digits and not just any chars like all other solutions so far do.
Gordon
@Gordon: When I (crudely) benchmarked memory usage soulmerge's explode with a limit of 1 only used 90 bytes more -- but that's well worth it considering you actually get to keep the first number! I agree the difference in performance is totally negligible. And yes you do have the only answer which removes digits only.
Josh
+7  A: 

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);
soulmerge
This answer is way underrated! It's by far the best solution IMHO.
Josh
like the explode with limit
elias
+2  A: 

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);
Karthik
A: 
substr(strstr("8,5,8,10,15,20,27,25,60,31,25,39,25,31",","),1);
nik
A: 

i think you will find they dont come more efficient than this

$mystring = "8,5,8,10,15,20,27,25,60,31,25,39,25,31";
$mystringbits = explode($mystring, ",");
for($i = $mystringbits.length-1; $i>0; i--)
{
   if($i == 0)
     array_pop($mystringbits);
}
$mystring = "";
for($j = 0; $j < $mystringbits.length; $j++)
{
   $mystring .= $mystringbits[$j] . ",";
}
echo $mystring;
DrLazer
My profiler says this is about 100 times slower than all other solutions, so what makes you think it's efficient?
Gordon
Uh, yes they do. Not only is your answer malformed PHP (`$mystringbits.length`???) it's also the slowest answer I have tested.
Josh
it was a joke.why would you bother profiling a solution that loops backwards through an array to take the first element out then forwards through the same array to join a string back togetherduhhhhhhhhhhhhhh *clap* *clap* *clap*
DrLazer
Why would you bother posting a joke as an answer to a serious question? You deserve the downvotes you got.
Josh