ashutoash Posted May 9, 2011 Share Posted May 9, 2011 All, I have a part of code where I get a string or numbers like (234,2345,4536,45,75657...). Can you guys help figure out how I can get the last element of that particular string..i.e., i want to echo 75657 to screen and the likes. The last number is not a fixed size one. It can be a n digit number. I tried using array_pop and end. But it did not work. Suggestion or code snippets please. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted May 9, 2011 Share Posted May 9, 2011 echo substr(strrchr($string, ','), 1); Quote Link to comment Share on other sites More sharing options...
ashutoash Posted May 9, 2011 Author Share Posted May 9, 2011 @Abraca: I tried it, it doesn't work though!!! Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2011 Share Posted May 9, 2011 $number = array_pop(explode(',',$string)); Quote Link to comment Share on other sites More sharing options...
ashutoash Posted May 9, 2011 Author Share Posted May 9, 2011 Thanks Crayon...it worked ;) Quote Link to comment Share on other sites More sharing options...
ashutoash Posted May 9, 2011 Author Share Posted May 9, 2011 @Crayon: What would the expression be if I want to get the first element of the string? Quote Link to comment Share on other sites More sharing options...
ashutoash Posted May 9, 2011 Author Share Posted May 9, 2011 never mind...figured it out. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted May 9, 2011 Share Posted May 9, 2011 @Abraca: I tried it, it doesn't work though!!! Sure it does: $string = '234,2345,4536,45,75657'; echo substr(strrchr($string, ','), 1); 75657 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.