Jump to content


Photo

counting numbers \ letters


  • Please log in to reply
6 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 12 September 2006 - 11:09 AM

Dear all

Is 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 value

HE11O22

then it would count the numbers as 4 (1122) and the letters as 3 (HEO)

Is this possible?

Thanks in advance

Ed

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 September 2006 - 11:22 AM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 12 September 2006 - 11:24 AM

There are several ways you can go about this, I've picked this method at random:
<?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";
?>


#4 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 12 September 2006 - 11:32 AM

Thank you

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 11:37 AM

Easiest/quickest way:

<?php

$string = 'HE11O22';

$numAlpha = strlen(preg_replace('/[^a-z]+?/i', '', $string));
$numNumerical = strlen(preg_replace('/[^0-9]+?/i', '', $string));

?>


#6 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 12 September 2006 - 12:11 PM

Hi

Thanks for all the help, following this, if I had the following value

45 ROADE

Is 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 was

45 4ROADE

and it would return "no" if the value was

45 ROADE

Not sure if this is possible though, although, you guys seem to be able to resolve most questions.

Thanks in advance for any further help

Cheers

Ed

#7 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 12 September 2006 - 01:13 PM

For your specific example (and for similarly formatted examples), this should work:
<?php
$str = "45 ROADE";
$tmp = explode(" ",$str);
echo (is_numeric($tmp[1]{0})) ? "Yes" : "No";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users