PHP fix base64_decode incorrect decoding error

Fix a garbled base64 decoded string by trying this base64 url decoder:

function base64url_decode($base64url) {
    // atob(s.replace(/-/g, '+').replace(/_/g, '/'));
    $base64 = strtr($base64url, '-_', '+/');
    $plain_text = base64_decode($base64);
    return $plain_text;

Why? The mapping table of the base64 implementations vary. For example, the Gmail API uses "-" and "_" as the last two characters (RFC 4648).

Keywords: base64, base64_encode, base64_decode, atob, btoa, JavaScript

Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment