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,开发者_StackOverflow31,25,39,25,31,26,28,80,28,27,31,27,29,26,35
How would I remove the "8," from the string? Thanks.
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.
$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);
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);
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);
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);
substr(strstr("8,5,8,10,15,20,27,25,60,31,25,39,25,31",","),1);
Like this works if '8' is bigger than 9!
$numbers = substr($numbers, strpos($text,",") + 1);
精彩评论