Created
March 25, 2012 00:51
-
-
Save stefanocudini/2190465 to your computer and use it in GitHub Desktop.
base64 encode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
function charCodeAt($str, $i) | |
{ | |
return ord(substr($str, $i, 1)); | |
} | |
function textToBase64($t) | |
{ | |
$tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
$r=''; $m=0; $a=0; $tl=strlen($t)-1; | |
for($n=0; $n<=$tl; $n++) | |
{ | |
$c = charCodeAt($t,$n); | |
$r .= substr($tab, (($c << $m | $a) & 63) ,1); | |
$a = $c >> (6-$m); | |
$m+=2; | |
if($m==6 || $n==$tl) { | |
$r .= substr($tab, $a ,1); | |
$m=0; | |
$a=0; | |
} | |
} | |
return $r; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment