Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created March 25, 2012 00:51
Show Gist options
  • Save stefanocudini/2190465 to your computer and use it in GitHub Desktop.
Save stefanocudini/2190465 to your computer and use it in GitHub Desktop.
base64 encode
<?
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