Created
October 27, 2021 03:05
-
-
Save dangdungcntt/8429f058bd1867ecfaca83b865805dcf to your computer and use it in GitHub Desktop.
PHP Helper functions for Safe Base64 URL 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
<?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