`-` (minus) * + position #63: `/` -> `_` (underscore) * * @param string $value raw value * @return string base64url encoded string */ public static function base64urlEncode(string $value): string { return strtr(base64_encode($value), ['+' => '-', '/' => '_', '=' => '']); } /** * Returns base64 decoded value with a URL and filename safe alphabet * according to https://tools.ietf.org/html/rfc4648#section-5 * * The difference to classic base64 is, that the result * alphabet is adjusted like shown below, padding (`=`) * is stripped completely: * + position #62: `-` (minus) -> `+` * + position #63: `_` (underscore) -> `/` * * @param string $value base64url decoded string * @param bool $strict enforces to only allow characters contained in the base64(url) alphabet * @return string|false raw value, or `false` if non-base64(url) characters were given in strict mode */ public static function base64urlDecode(string $value, bool $strict = false): string|false { return base64_decode(strtr($value, ['-' => '+', '_' => '/']), $strict); } /** * Explodes a string while respecting escape characters * * e.g.: delimiter: '.'; escapeCharacter: '\'; subject: 'new\.site.child' * result: [new.site, child] * @param string $delimiter * @param string $subject * @param string $escapeCharacter */ public static function explodeEscaped(string $delimiter, string $subject, string $escapeCharacter = '\\'): array { if ($delimiter !== '') { $placeholder = '\\0\\0\\0_esc'; $subjectEscaped = str_replace($escapeCharacter . $delimiter, $placeholder, $subject); $escapeParts = explode($delimiter, $subjectEscaped); foreach ($escapeParts as &$part) { $part = str_replace($placeholder, $delimiter, $part); } return $escapeParts; } return [$subject]; } }