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. 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! } 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? 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 } ?> 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"; 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. 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. 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. 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? 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."; } ?> 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. Link to comment https://forums.phpfreaks.com/topic/67481-solved-string-numeric/#findComment-338845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.