Base36, Base64 or Base16 functions in PHP
Language: English
Programming Language: PHP
Published by: shanx24 [not registered]
Last Update: 5/15/2006
Views: 1566
Description
Base calculation functions implemented in PHP -- Base16, Base36 or Base64.
Code
1 <?
2
3 /**
4 * Function to calculate base36 values from a number
5 *
6 * @param $value The number
7 * @param $base The base to be applied (16, 36 or 64)
8 * @return The calculated string
9 * @author Shashank Tripathi (shanx@shanx.com)
10 * @version 0.1 - Let me know if something doesnt work
11 *
12 */
13
14 function base36($value, $base)
15 {
16 $baseChars = array('0', '1', '2', '3', '4', '5',
17 '6', '7', '8', '9', 'a', 'b',
18 'c', 'd', 'e', 'f', 'g', 'h',
19 'i', 'j', 'k', 'l', 'm', 'n',
20 'o', 'p', 'q', 'r', 's', 't',
21 'u', 'v', 'w', 'x', 'y', 'z'
22 );
23
24 $remainder = 0;
25 $newval = "";
26
27 while ( $value > 0 )
28 {
29 $remainder = $value % $base;
30 $value = ( ($value - $remainder)/ $base );
31 $newval .= $baseChars[$remainder];
32 }
33 return strrev($newval);
34
35 }
36
37
38
39 echo "The string for 46655, for instance, is " . base36(46655, 36);
40
41 ?>
No comments avaiable
Add a comment
Name *
Email (won't be displayed) *
Website
Comment *
Security Code *