note 54256 added to function.chr

From: Date: Tue, 28 Jun 2005 14:43:22 +0000
Subject: note 54256 added to function.chr
Groups: php.notes 
Request: Send a blank email to php-notes+get-91095@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.chr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54256 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54256&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54256&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54256&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54256&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54256&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54256&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54256&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91095) next »