Skip to content

Instantly share code, notes, and snippets.

@dangdungcntt
Created October 27, 2021 03:05
Show Gist options
  • Save dangdungcntt/8429f058bd1867ecfaca83b865805dcf to your computer and use it in GitHub Desktop.
Save dangdungcntt/8429f058bd1867ecfaca83b865805dcf to your computer and use it in GitHub Desktop.
PHP Helper functions for Safe Base64 URL encode
<?php
/*
* Copied from https://github.com/firebase/php-jwt/blob/feb0e820b8436873675fd3aca04f3728eb2185cb/src/JWT.php#L350
*/
function encodeURLSafe($data): string
{
return \str_replace('=', '', \strtr(\base64_encode($data), '+/', '-_'));
}
/*
* Copied from https://github.com/firebase/php-jwt/blob/feb0e820b8436873675fd3aca04f3728eb2185cb/src/JWT.php#L333
*/
function decodeURLSafe($data): string|false
{
$remainder = \strlen($data) % 4;
if ($remainder) {
$padlen = 4 - $remainder;
$data .= \str_repeat('=', $padlen);
}
return \base64_decode(\strtr($data, '-_', '+/'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment