# Need a string algorithm.

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I need to assign all characters the values 1-26, and add them up in
the string, each time multiplying the next characters value by
10^string placement.  It should look something like this:

Lets try an example:

C          3
O       150
R     1,800
E     5,000
Y  250,000
----------------
256,953

The end result of the word Corey is the number 256,953.

Can anyone get this working in php?  It is some new code I want to run
on my website http://www.ckoogle.com

## Re: Need a string algorithm.

CoreyWhite wrote:

\$multiplier = 1;
\$sum = 0;
for(\$i=0; \$i<length(\$s); \$i++) {
\$c = substr(\$s, \$i, 1);
\$cv = strpos("ABCDEFGHIJKLMNOPQRSTUVWXY", \$c);
\$sum = \$sum+\$cv*\$multiplier;
\$multiplier *= 10;
}

N.B. This is untested code. The resulting numbers will not be unique;
consider the string "AN" (25) and "BE" (25).

## Re: Need a string algorithm.

// \$my_word in [a-zA-Z] !!!
function codeMyWord(\$my_word) {
\$word_coded = 0;
for (\$i = 0; \$i < strlen(\$my_word); \$i++) {
\$word_coded += (ord(strtoupper(\$my_word)) - 64) * pow(10, \$i);
}
return number_format(\$word_coded);
}
echo codeMyWord('CoreyWhite');  //-->   7,100,556,953

--
@@@@@
E -00       comme on est very beaux dis !
'   `)   /
|\_ =="

## Re: Need a string algorithm.

wrote:

sounds like someone trolling for homework
really lame

## Re: Need a string algorithm.

oldami kirjoitti:

Nah, he's just mad as a hat. We're talking about the guy who claims to
have solved the Da Vinci code.

--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."

## Re: Need a string algorithm.

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--