Pages

Feb 1, 2011

php url-safe base64 encoding

Basically:
  • '+' is replaced by '-'
  • '/' is replaced by '_'(underscore)
  • '=' is replaced by '!'

function urlsafe_b64encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;
}

function urlsafe_b64decode($string) {
    $data = str_replace(array('-','_'),array('+','/'),$string);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    return base64_decode($data);
}

No comments:

Post a Comment