Skip to content

Core\Encoding::toBase64Url

Core\Encoding::toBase64Url(bytes $b): stringCore\Encoding::toBase64Url(
    bytes $b,
): string

Writes bytes as base64 text that is safe in a web address.

This form uses the letters A to Z in both cases, the digits 0 to 9, and the two symbols - and _. It writes no = padding at all. Every one of those characters is safe in a URL, in a query string and in a file name. A JSON Web Token is written this way too, and that is where most people meet the form. Every byte has a spelling, so this member never fails, and the empty buffer gives the empty string.

The text is still a third longer than the buffer, and it still grows three bytes at a time. Without the padding its length can be any number except one more than a multiple of four.

Good to know: Core\Encoding::fromBase64Url reads this text back. It reads this form only, so text written by Core\Encoding::toBase64 has to go back through Core\Encoding::fromBase64.

Parameters

  • $bbytesThe bytes to encode.

Return value

Returns string. The URL-safe base64 text. Empty bytes give the empty string.

Examples

Text with nothing to escape

<?nvs
// Shows the round trip: bytes go out as text that is safe in an address, and
// the same bytes come back.
bytes $raw = Core\Encoding::encodeText('hello', Core\Charset::Utf8);
// Five bytes become seven characters, and nothing is added at the end. This
// prints aGVsbG8.
echo Core\Encoding::toBase64Url($raw), "\n";
// `Core\Encoding::fromBase64Url` reads the text back. This prints hello.
bytes $back = Core\Encoding::fromBase64Url(Core\Encoding::toBase64Url($raw));
echo Core\Encoding::decodeText($back, Core\Charset::Utf8), "\n";
// Three bytes fill a group exactly, and these three reach no symbol. So both
// forms write the same text here. This prints YWJj YWJj.
bytes $three = Core\Encoding::encodeText('abc', Core\Charset::Utf8);
echo Core\Encoding::toBase64Url($three), ' ', Core\Encoding::toBase64($three), "\n";
// The empty buffer gives the empty string, so this line prints nothing.
echo Core\Encoding::toBase64Url(Core\Encoding::encodeText('', Core\Charset::Utf8)), "\n";
Output
aGVsbG8
hello
YWJj YWJj

The two differences

<?nvs
// Shows the two differences from `Core\Encoding::toBase64`, and why they
// matter in a web address.
bytes $raw = Core\Encoding::fromHex('fbff');
// The first difference is the alphabet. These two bytes reach both symbols,
// so this prints +/8= and then -_8.
echo Core\Encoding::toBase64($raw), "\n";
echo Core\Encoding::toBase64Url($raw), "\n";
// In a query string `+` means a space, and `/` starts a new part of a path.
// So the first form changes meaning inside an address. The second one does not.
// The second difference is the padding, which this form does not write. So the
// length is not always a multiple of four. This prints 0 2 3 4 6 for sizes 0 to 4.
uint $size = 0;
while ($size <= 4) {
echo Core\Str::length(Core\Encoding::toBase64Url(Core\Bytes::fill($size, 65))), ' ';
$size = $size + 1;
}
echo "\n";
Output
+/8=
-_8
0 2 3 4 6

An identifier inside a link

<?nvs
// Shows the everyday use: bytes become part of a link, where `+`, `/` and `=`
// would each be read as something else.
// Sixteen bytes that name one download. A real program takes these from
// `Core\Random::bytes` so that nobody can guess the next one.
bytes $id = Core\Encoding::fromHex('3f9d2b7e5c1a48d0ff00e6a19b4c7d82');
// Sixteen bytes become 22 characters, and every one of them is safe here.
// This prints the link and then 22.
string $slug = Core\Encoding::toBase64Url($id);
echo 'https://example.com/files/', $slug, "\n";
echo Core\Str::length($slug), "\n";
// The program that answers the request reads the same sixteen bytes back and
// looks the download up by them. This prints 16 0.
bytes $received = Core\Encoding::fromBase64Url($slug);
echo Core\Bytes::length($received), ' ', Core\Bytes::compare($received, $id), "\n";
Output
https://example.com/files/P50rflwaSND_AOahm0x9gg
22
16 0