note 23062 added to ref.math
| From: | shanx at shanx dot com | Date: | Mon, 08 Jul 2002 17:13:03 +0000 |
| Subject: | note 23062 added to ref.math | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-32740@lists.php.net to get a copy of this message | ||
<?
/**
* Function to calculate base36 values from a number. Very
* useful if you wish to generate IDs from numbers.
*
* @param $value The number
* @param $base The base to be applied (16, 36 or 64)
* @return The calculated string
* @author Shashank Tripathi (shanx@shanx.com)
* @version 0.1 - Let me know if something doesnt work
*
*/
function base36($value, $base)
{
$baseChars = 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'
);
$remainder = 0;
$newval = "";
while ( $value > 0 )
{
$remainder = $value % $base;
$value = ( ($value - $remainder)/ $base );
$newval .= $baseChars[$remainder];
}
return strrev($newval);
}
echo "The string for 46655, for instance, is " . base36(46655, 36);
?>
--
http://www.php.net/manual/en/ref.math.php
http://master.php.net/manage/user-notes.php?action=edit+23062
http://master.php.net/manage/user-notes.php?action=delete+23062
http://master.php.net/manage/user-notes.php?action=reject+23062