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. Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/ Share on other sites More sharing options...
AbraCadaver Posted May 9, 2011 Share Posted May 9, 2011 echo substr(strrchr($string, ','), 1); Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/#findComment-1212911 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!!! Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/#findComment-1212919 Share on other sites More sharing options...
.josh Posted May 9, 2011 Share Posted May 9, 2011 $number = array_pop(explode(',',$string)); Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/#findComment-1212923 Share on other sites More sharing options...
ashutoash Posted May 9, 2011 Author Share Posted May 9, 2011 Thanks Crayon...it worked ;) Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/#findComment-1212939 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? Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/#findComment-1213007 Share on other sites More sharing options...
ashutoash Posted May 9, 2011 Author Share Posted May 9, 2011 never mind...figured it out. Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/#findComment-1213012 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 Link to comment https://forums.phpfreaks.com/topic/235945-help-with-string-manipulation/#findComment-1213015 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.