/    Sign up×
Community /Pin to ProfileBookmark

assigning letters to numbers

hi, i want to know if ther’s a way to assign letters to numbers????? e.g.
a=1,
b=2,
c=3 and so on and so on.

and then get an input from a textbox which is a word/name convert it to these numbers and add it all up (and may b convert it to a variable) to echo the result…… i’m not sure if this is possible and its not reli that needed but it’dd really help if i could do it……. does anyone have a way to do this and is it even possible…….

thx, JAM

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guyDec 22.2005 — Well I don't know of a function, there would be a couple of ways to do this though. I think I will just do the most obvious for you - it may be a bit large though.[code=php]$name = strtolower($_POST['name']);
$name = str_split($name);
$value = 0;

foreach ($name as $letter){
switch ($letter){
case 'a':
$value += 1;
break;
case 'b':
$value += 2;
break;
case 'c':
$value += 3;
break;
case 'd':
$value += 4;
break;
case 'e':
$value += 5;
break;
case 'f':
$value += 6;
break;
case 'g':
$value += 7;
break;
case 'h':
$value += 8;
break;
case 'i':
$value += 9;
break;
case 'j':
$value += 10;
break;
case 'k':
$value += 11;
break;
case 'l':
$value += 12;
break;
case 'm':
$value += 13;
break;
case 'n':
$value += 14;
break;
case 'o':
$value += 15;
break;
case 'p':
$value += 16;
break;
case 'q':
$value += 17;
break;
case 'r':
$value += 18;
break;
case 's':
$value += 19;
break;
case 't':
$value += 20;
break;
case 'u':
$value += 21;
break;
case 'v':
$value += 22;
break;
case 'w':
$value += 23;
break;
case 'x':
$value += 24;
break;
case 'y':
$value += 25;
break;
case 'z':
$value += 26;
break;
default:
break;
}
}
echo $value;[/code]
Copy linkTweet thisAlerts:
@NogDogDec 22.2005 — [code=php]
function get_char_value($char)
{
$char = strtoupper($char);
if(preg_match('/[A-Z]/', $char))
{
return(ord($char) - 64);
}
if(preg_match('/[0-9]/', $char))
{
return($char);
}
else
{
return(0); // all non-alphanumerics will be 0
}
}
$string = "This is a test.";
$count = 0;
for($i=0; $i<strlen($string); $i++)
{
$count += get_char_value($string{$i});
}
echo "<p>Total: $count</p>n";
[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsDec 22.2005 — I was just about to sugest the same method as Nog.
Copy linkTweet thisAlerts:
@jamauthorDec 22.2005 — thanks bathurst guy i think u nailed what i wanted there........and cheers nog i havent tryed that yet but it looks like it'l work with less code than bathurst guy's thx 2 both of you..... JAM
×

Success!

Help @jam spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...