Jump to content

counting numbers \ letters


CanMan2004

Recommended Posts

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
Link to comment
Share on other sites

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
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.