almightyegg Posted December 22, 2007 Share Posted December 22, 2007 If I have a $variable how would select just the last character. So if it was thomas, it would select s ?? Any help is greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/82771-solved-selecting-the-last-character-of-a-variable/ Share on other sites More sharing options...
corbin Posted December 22, 2007 Share Posted December 22, 2007 Hmmm a few ways to do this.... Here are the first two that come to mind.... Not sure which is faster, but it's probably the first. $word = 'thomas'; $s = $word[strlen($word)-1]; $word = 'thomas'; $l = strlen($word); $s = sub_str($word, $l-2, $l-1); Quote Link to comment https://forums.phpfreaks.com/topic/82771-solved-selecting-the-last-character-of-a-variable/#findComment-420970 Share on other sites More sharing options...
almightyegg Posted December 22, 2007 Author Share Posted December 22, 2007 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/82771-solved-selecting-the-last-character-of-a-variable/#findComment-420972 Share on other sites More sharing options...
corbin Posted December 22, 2007 Share Posted December 22, 2007 Hrmmm just had a brain slip.... It's substr not sub_str, and I think this would work too: $word = 'thomas'; $s = sub_str($word, -1); Quote Link to comment https://forums.phpfreaks.com/topic/82771-solved-selecting-the-last-character-of-a-variable/#findComment-420973 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.