jeff5656 Posted November 21, 2011 Share Posted November 21, 2011 How can I test if a string has a number at the end? i.e. smith1 instead of smith? I won't know how long the string is or whether there is more than 1 number but I will know that the string's last character is a number. Thank you! Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/ Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 You could use substr to grab the last character and then check it's type... also check out ctype functions here: http://pt.php.net/manual/en/ref.ctype.php Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/#findComment-1290052 Share on other sites More sharing options...
jeff5656 Posted November 21, 2011 Author Share Posted November 21, 2011 but with substr() don't I need to know the length of the string? With my situation I won't know the string length - only that I need to check the very last character. Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/#findComment-1290059 Share on other sites More sharing options...
Pikachu2000 Posted November 21, 2011 Share Posted November 21, 2011 No, the manual explains how to get the last character. Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/#findComment-1290064 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 read the manual (I gave you the link) the very first example sorts out that problem. Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/#findComment-1290066 Share on other sites More sharing options...
jeff5656 Posted November 21, 2011 Author Share Posted November 21, 2011 Excuse my ignorance here is example 1 from your link $rest = substr("abcdef", -1); It does answer how to identify the last character, but how do I know if the last string is a *number*? Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/#findComment-1290074 Share on other sites More sharing options...
WebStyles Posted November 21, 2011 Share Posted November 21, 2011 read the rest of my fist post. I gave you a link to a list of ctype functions that will sort that out for you. Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/#findComment-1290084 Share on other sites More sharing options...
jeff5656 Posted November 21, 2011 Author Share Posted November 21, 2011 oops! I should have clicked that second link. Thanks. Link to comment https://forums.phpfreaks.com/topic/251553-number-at-end-of-string/#findComment-1290087 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.