Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 Okay say I have this string: $string = "hello world"; What I want to do is go by every letter in a string and see if there is a number in there. Any ideas how to do this. I can't use charAt in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/ Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 foreach ($string as $char) { if ($char > 47 && $char < 58) //it has a number in it! } Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338770 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Author Share Posted August 31, 2007 o.O And how does $char know how long the $string is? Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338786 Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 just use preg_match(); or <?php $string = "Hello World"; if(is_numeric($string)){ // Runs this script if there is a number } else { // Runs this script if there isnt } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338791 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Author Share Posted August 31, 2007 But $string isn't numeric, I want to see if there is a number in a string. Like this would return true: $string = "Hello W0rld"; Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338796 Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 WHAT? it will check if there is a number, and if it is, it will return true, else it will return false.. explain what you want. Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338798 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Author Share Posted August 31, 2007 What I meant is is_numeric doesn't work because it checks to see if the $string is numeric, which it isn't. It doesn't check every character in the string. Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338802 Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Author Share Posted August 31, 2007 Nevermind. I figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338820 Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 May I see your code? Please? Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338831 Share on other sites More sharing options...
pocobueno1388 Posted August 31, 2007 Share Posted August 31, 2007 phpSensei - Ken wants to check if their is a number IN the string, not if the entire variable is numeric or not. Thats why is_numeric won't work for the situation. The code probably ended up being something like this: <?php $str = "dsffdaf"; $str = str_split($str); foreach ($str as $char){ if (is_numeric($char)){ $nums = TRUE; break; } } if (isset($nums)){ echo "The string contained a number."; } else { echo "The string has NO numbers."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338840 Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 oh sorry mate, I understood you wrong. Quote Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338845 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.