Last active
August 7, 2023 09:36
-
-
Save yusureabc/ea944250a54e123491ca163353305e70 to your computer and use it in GitHub Desktop.
URL-safe Base64
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 | |
/** | |
* Decode a string with URL-safe Base64. | |
* | |
* @param string $input A Base64 encoded string | |
* | |
* @return string A decoded string | |
*/ | |
function urlsafeB64Decode($input) | |
{ | |
$remainder = strlen($input) % 4; | |
if ($remainder) { | |
$padlen = 4 - $remainder; | |
$input .= str_repeat('=', $padlen); | |
} | |
return base64_decode(strtr($input, '-_', '+/')); | |
} | |
/** | |
* Encode a string with URL-safe Base64. | |
* | |
* @param string $input The string you want encoded | |
* | |
* @return string The base64 encode of what you passed in | |
*/ | |
function urlsafeB64Encode($input) | |
{ | |
return str_replace('=', '', strtr(base64_encode($input), '+/', '-_')); | |
} | |
if (! function_exists('url_safe_base64_encode')) { | |
function url_safe_base64_encode ($data) { | |
return str_replace(array('+','/', '='),array('-','_', ''), base64_encode($data)); | |
} | |
} | |
if (! function_exists('url_safe_base64_decode')) { | |
function url_safe_base64_decode ($data) { | |
$base_64 = str_replace(array('-','_'),array('+','/'), $data); | |
return base64_decode($base_64); | |
} | |
} | |
if (! function_exists('base64_to_url_safe_base64')) { | |
function base64_to_url_safe_base64 ($data) { | |
return str_replace(array('+', '/', '='),array('-', '_', ''), $data); | |
} | |
} | |
if (! function_exists('url_safe_base64_to_base64')) { | |
function url_safe_base64_to_base64 ($data) { | |
return str_replace(array('-','_'),array('+','/'), $data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment