Warptweet Posted December 18, 2007 Share Posted December 18, 2007 I have some forms on my site, and some of them require that you can only type in numbers, that means no decimals either. How can I check a string to see if it contains letters, decimals, or any of those complicated signs. AKA: Check if theres anything other than numbers 1-9 Thank you so much for any help! Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/ Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 if(preg_match('/^[0-9]+$')) { //1 or more numbers } Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417282 Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 if(preg_match('/^[0-9]+$')) { //1 or more numbers } nope easier way ctype_digit(texthere) Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417285 Share on other sites More sharing options...
Warptweet Posted December 18, 2007 Author Share Posted December 18, 2007 Thanks both of you. I used tengs method because I actually understood his, but thanks corbin too for trying. Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417288 Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Ahhh, mine was just a regular expression.... I didn't think to use teng's method.... Extending on teng's, you could also do: $input = $_POST['input']; $iinput = (int) $input; if($input != $iinput) { //not numeric } That might cause PHP to throw a notice or warning though.... Not sure. Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417290 Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 Ahhh, mine was just a regular expression.... I didn't think to use teng's method.... Extending on teng's, you could also do: $input = $_POST['input']; $iinput = (int) $input; if($input != $iinput) { //not numeric } That might cause PHP to throw a notice or warning though.... Not sure. wont work because int will remove ZERO in front of your string right? eg.. this will give (int)' 0005' >>>5 Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417295 Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Ohhh good call.... Didn't think of that.... I guess you could ltrim($input, '0') and then compare, but that would be much slower..... Hmmm.... I hadn't ever heard of ctype_digit before hehe.... Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417299 Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 yah.. ctype data is cool Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417302 Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 It's also extremely fast.... It took half the time as converting the input to int and left trimming 0s on my comp. It even beat: $iput = (int) $input; if($iput == 0 && $input != 0) { } By .09 seconds with 100,000 runs.... Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417306 Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 Or is_numeric Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417309 Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 Or is_numeric nope it will accept decimals is_numeric(0.343); // true Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417311 Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 You could use is_numeric and strpos($input, '.'). Oddly enough, is_numeric combined with strpos is like .0001 seconds faster when the input isn't numeric, but when it is numeric, thus it has to continue to the strpos check, it's like 2.2 times slower lol.... Link to comment https://forums.phpfreaks.com/topic/82118-solved-check-if-string-contains-anything-other-than-numbers/#findComment-417312 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.