dreamwest Posted May 31, 2009 Share Posted May 31, 2009 How can i check if a string of numbers is numeric, the example below is apparently still not numeric $string = 13530 21333 16945 22819 24170 31196 19209; Quote Link to comment https://forums.phpfreaks.com/topic/160360-is_numeric/ Share on other sites More sharing options...
thebadbad Posted May 31, 2009 Share Posted May 31, 2009 A string needs quotes. And you can use preg_match(), allowing digits and whitespace: <?php $string = '13530 21333 16945 22819 24170 31196 19209'; if (preg_match('~^[0-9\s]+$~D', $string)) { //string only contains digits and/or whitespace } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160360-is_numeric/#findComment-846219 Share on other sites More sharing options...
webref.eu Posted May 31, 2009 Share Posted May 31, 2009 Yes, you would have to remove spaces from your string, as I don't think your string will be recognised as a number if it has spaces in. You'd have to write a routine to remove spaces and then you can check for a number as follows: //Protection from hackers. Check ProductId is just a number $TestForNumber = is_numeric($ProductId); If ($TestForNumber == 0) { echo "Sorry, the Product Id tried is not allowed."; exit(); } Rgds Quote Link to comment https://forums.phpfreaks.com/topic/160360-is_numeric/#findComment-846224 Share on other sites More sharing options...
dreamwest Posted May 31, 2009 Author Share Posted May 31, 2009 A string needs quotes. And you can use preg_match(), allowing digits and whitespace: <?php $string = '13530 21333 16945 22819 24170 31196 19209'; if (preg_match('~^[0-9\s]+$~D', $string)) { //string only contains digits and/or whitespace } ?> Thanks. I gotta learn preg match one day Quote Link to comment https://forums.phpfreaks.com/topic/160360-is_numeric/#findComment-846228 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.