note 54256 deleted from function.chr by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Sep 2010 21:15:16 +0000 |
| Subject: | note 54256 deleted from function.chr by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172600@lists.php.net to get a copy of this message | ||
Note Submitter: Mike
----
Hi out there!
Here are two usefull functions to convert ordinal values to int value and back (unlimited length).
For Example:
'A' = 1
'B' = 2
'C' = 3
'AA' = 27
'ABC' = 731
Convert ordinal value to INT:
function convertAlphabetToInt($alpha_string) {
$int_wert=0;
$potenzcounter=0;
for ($i=strlen($alpha_string);$i>0;$i--) {
$ordinalwert=(ord(substr($alpha_string,$i-1,1))-64);
$int_wert+=$ordinalwert*pow(26,$potenzcounter);
$potenzcounter++;
}
return $int_wert;
}
And back from INT to ordinal:
function convertIntToAlphabet($int_wert) {
if($int_wert%26>=1) {
$alpha_string=chr(($int_wert%26)+64).$alpha_string;
$alpha_string=convertIntToAlphabet($int_wert/26).$alpha_string;
}
return $alpha_string;
}
This functions will return for example:
convertAlphabetToInt("ABCD") => Output: 19010
convertIntToAlphabet(19010) = Output: "ABCD"
I hope someone can use it :)
Greetings,
Mike