CanMan2004 Posted September 12, 2006 Share Posted September 12, 2006 Dear allIs there a way to count the number of numbers in a value and the number of letters in a value? I know you can count the total number of letters and numbers, what im looking for is something that if I had the valueHE11O22then it would count the numbers as 4 (1122) and the letters as 3 (HEO)Is this possible?Thanks in advanceEd Link to comment https://forums.phpfreaks.com/topic/20493-counting-numbers-letters/ Share on other sites More sharing options...
redarrow Posted September 12, 2006 Share Posted September 12, 2006 There are meny ways to do this, You can brake the code down in an array and count with strlen or you could use eregi and count with strlen even other ways.good luck. Link to comment https://forums.phpfreaks.com/topic/20493-counting-numbers-letters/#findComment-90327 Share on other sites More sharing options...
zq29 Posted September 12, 2006 Share Posted September 12, 2006 There are several ways you can go about this, I've picked this method at random:[code]<?php$string = strtolower("HE11O22");$letters = 0;$numbers = 0;for($i=0; $i<strlen($string); $i++) { if(in_array($string{$i},range("a","z"))) $letters++; elseif(in_array($string{$i},range("0","9"))) $numbers++;}echo "Letters: $letters<br/>Numbers: $numbers";?>[/code] Link to comment https://forums.phpfreaks.com/topic/20493-counting-numbers-letters/#findComment-90328 Share on other sites More sharing options...
CanMan2004 Posted September 12, 2006 Author Share Posted September 12, 2006 Thank you Link to comment https://forums.phpfreaks.com/topic/20493-counting-numbers-letters/#findComment-90331 Share on other sites More sharing options...
Jenk Posted September 12, 2006 Share Posted September 12, 2006 Easiest/quickest way:[code]<?php$string = 'HE11O22';$numAlpha = strlen(preg_replace('/[^a-z]+?/i', '', $string));$numNumerical = strlen(preg_replace('/[^0-9]+?/i', '', $string));?>[/code] Link to comment https://forums.phpfreaks.com/topic/20493-counting-numbers-letters/#findComment-90333 Share on other sites More sharing options...
CanMan2004 Posted September 12, 2006 Author Share Posted September 12, 2006 HiThanks for all the help, following this, if I had the following value45 ROADEIs there a way for php to tell if the 2nd word (ROADE) starts with a number or a letter and then flag up yes or no if it does.For example, it would return "yes" if the value was45 4ROADEand it would return "no" if the value was45 ROADENot sure if this is possible though, although, you guys seem to be able to resolve most questions.Thanks in advance for any further helpCheersEd Link to comment https://forums.phpfreaks.com/topic/20493-counting-numbers-letters/#findComment-90352 Share on other sites More sharing options...
zq29 Posted September 12, 2006 Share Posted September 12, 2006 For your specific example (and for similarly formatted examples), this should work:[code]<?php$str = "45 ROADE";$tmp = explode(" ",$str);echo (is_numeric($tmp[1]{0})) ? "Yes" : "No";?>[/code] Link to comment https://forums.phpfreaks.com/topic/20493-counting-numbers-letters/#findComment-90378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.