fix3r Posted May 12, 2007 Share Posted May 12, 2007 i need to find the last digit of a string. for example $pos = 1287120; I need to check if the last digit is "0". The number could be any length but I am positive it will be a number. How would I do this? Quote Link to comment https://forums.phpfreaks.com/topic/51028-finding-last-digit-of-a-string/ Share on other sites More sharing options...
Nameless12 Posted May 12, 2007 Share Posted May 12, 2007 Just cast the integer to a string and then get the last char the same way you would in any other string, there are various ways you can do this the easiest is probably using "[]" with strlen() Quote Link to comment https://forums.phpfreaks.com/topic/51028-finding-last-digit-of-a-string/#findComment-251106 Share on other sites More sharing options...
fix3r Posted May 12, 2007 Author Share Posted May 12, 2007 Just cast the integer to a string and then get the last char the same way you would in any other string, there are various ways you can do this the easiest is probably using "[]" with strlen() Sorry, maybe you misunderstood. I really have no clue how to actually get the last character of ANY string. That's what I am asking for. Thank you for responding though. Hopefully you understand my question more clearly now. That was my fault. Quote Link to comment https://forums.phpfreaks.com/topic/51028-finding-last-digit-of-a-string/#findComment-251107 Share on other sites More sharing options...
Nameless12 Posted May 12, 2007 Share Posted May 12, 2007 you can select individual characters using "[]" $a = 'abc'; echo $a[0] //a echo $a[1] //b echo $a[2] //c strlen($a) // 3 echo $a[strlen($a) - 1] //c (the last character) Quote Link to comment https://forums.phpfreaks.com/topic/51028-finding-last-digit-of-a-string/#findComment-251109 Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 $pos = 1287120; substr($pos, -1, 1); will return a 0 substr('abcdef', -1, 1); //returns f substr('1685484315', -1, 1); //returns 5 http://www.php.net/manual/en/function.substr.php Quote Link to comment https://forums.phpfreaks.com/topic/51028-finding-last-digit-of-a-string/#findComment-251112 Share on other sites More sharing options...
Nameless12 Posted May 12, 2007 Share Posted May 12, 2007 substr($str, -1) works, there is no need to specify the length Quote Link to comment https://forums.phpfreaks.com/topic/51028-finding-last-digit-of-a-string/#findComment-251113 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.