| 
	
 | 
 Posted by Bob Stearns on 04/04/07 21:39 
CoreyWhite wrote: 
> 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 
>  
$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).
 
  
Navigation:
[Reply to this message] 
 |