note 56179 added to function.chr
| From: | php at elijahtech dot com | Date: | Fri, 26 Aug 2005 03:52:02 +0000 |
| Subject: | note 56179 added to function.chr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94203@lists.php.net to get a copy of this message | ||
Based on Mike's code, here are some handy functions to convert integer numbers into an
alphanumeric form based on a custom character set. Meaning, the array $chrs can include anything,
and as long as the same $chrs is used in the decoding, it will always work. I am using it a
condense a generated ID that gets displayed.
e.g., g5 = 997, 10000093 = FXtP -- Much shorter!
Elijah
-----------------------------------------------------------
$chrs = array('0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', 'a',
'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S' ,'T', 'U', 'V', 'W',
'X', 'Y', 'Z');
function int_to_alph($int, $chrs)
{
$base = sizeof($chrs);
do {
$alph = $chrs[($int % $base)] . $alph;
} while($int = intval($int / $base));
return $alph;
}
function alph_to_int($alph, $chrs)
{
$base = sizeof($chrs);
for($i = 0, $int = 0; $i < strlen($alph); $i++)
{
$int += intval(array_search(substr($alph, strlen($alph) - $i - 1, 1), $chrs)) * pow($base, $i);
}
return $int;
}
----
Manual Page -- http://www.php.net/manual/en/function.chr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56179
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56179&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56179&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56179&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56179&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56179&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56179&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56179&report=yes
Search -- http://master.php.net/manage/user-notes.php