Base64 for URL parameters/filenames, that adhere to RFC 4648.
Defaults to dropping the padding on encode since it's not required for decoding, and keeps the URL free of % encodings.
<?php
function base64url_encode($data, $pad = null) {
$data = str_replace(array('+', '/'), array('-', '_'), base64_encode($data));
if (!$pad) {
$data = rtrim($data, '=');
}
return $data;
}
function base64url_decode($data) {
return base64_decode(str_replace(array('-', '_'), array('+', '/'), $data));
}