Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first string before separator?

I have strings with folowing structure:

7_string_12
7_string2_122
7_string3_1223

How I can get string before second "_" ?

I want my final result to be :

7_string
7_string2
7_string3

I am using explode('_', $string) and combine first two values, but my script was very slow!

like image 873
dido Avatar asked Jan 10 '12 09:01

dido


1 Answers

$str = '7_string_12';
echo substr($str,0,strrpos($str,'_'));

echoes

7_string

no matter what's at the begining of the string

like image 123
k102 Avatar answered Sep 19 '22 21:09

k102