Skip to content

Instantly share code, notes, and snippets.

@jjeising
Last active May 15, 2018 19:10
Show Gist options
  • Save jjeising/230a3fe57a31d25a41855aecc094175e to your computer and use it in GitHub Desktop.
Save jjeising/230a3fe57a31d25a41855aecc094175e to your computer and use it in GitHub Desktop.
SAP ABAP GUID22 in PHP
<?php
/*
guid22 is base64 with a slightly changed alphabet (numbers first)
*/
function guid22(string $string): string {
static $alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/';
$length = strlen($string);
if ($length <= 0) {
return '';
}
$buffer = 0;
$pos = 0;
$result = '';
for ($i = 0; $i < $length; $i++) {
$buffer = ($buffer << 8) + ord($string[$i]);
$pos += 8;
while ($pos >= 6) {
$pos -= 6;
$result .= $alphabet[($buffer >> $pos) & 0x3f];
}
}
if ($pos > 0) {
$result .= $alphabet[$buffer << (6 - $pos) & 0x3f];
}
return str_pad($result, ceil(strlen($result) / 8) * 8, '=');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment