Oihana PHP

base64UrlEncode.php

Table of Contents

Functions

base64UrlEncode()  : string
Encodes binary data into a base64url string (RFC 4648 §5).

Functions

base64UrlEncode()

Encodes binary data into a base64url string (RFC 4648 §5).

base64UrlEncode(string $binary) : string

The output uses the URL- and filename-safe alphabet (- and _ instead of + and /) and omits the trailing = padding, producing the canonical URL-safe form used by JWT/JOSE, WebPush, signed URLs, etc.

The function is binary-safe: any byte sequence is accepted, including null bytes and arbitrary UTF-8.

Parameters
$binary : string

Raw bytes to encode. May be empty.

Tags
example
use function oihana\core\encoding\base64UrlEncode;

echo base64UrlEncode( 'hello' ) ;
// Outputs: aGVsbG8

echo base64UrlEncode( "\xFB\xFF" ) ;
// Outputs: -_8 (note the URL-safe '-' and '_', no '=' padding)

// JWT-style usage
$header  = base64UrlEncode( json_encode( [ 'alg' => 'HS256' , 'typ' => 'JWT' ] ) ) ;
$payload = base64UrlEncode( json_encode( [ 'sub' => '42' , 'iat' => time() ] ) ) ;
$signing = $header . '.' . $payload ;
see
base64UrlDecode()

For the inverse operation.

author

Marc Alcaraz (ekameleon)

since
1.0.8
Return values
string

The base64url-encoded representation, without padding. Always a subset of [A-Za-z0-9_-].

On this page

Search results