Skip to content

Instantly share code, notes, and snippets.

@Belphemur
Created June 14, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Belphemur/93e8f90138f2bebbe86f3ce109e0fa85 to your computer and use it in GitHub Desktop.
Save Belphemur/93e8f90138f2bebbe86f3ce109e0fa85 to your computer and use it in GitHub Desktop.
Salesforce ID 15 chars to 18 chars converter PHP
<?php
function to18char(string $inputId) {
$suffix = '';
for ($i = 0; $i < 3; $i++) {
$flags = 0;
for ($j = 0; $j < 5; $j++) {
$start = $i * 5 + $j;
$end = ($i * 5 + $j + 1) - $start;
$c = substr($inputId, $start, $end);
if (ctype_upper($c) && $c >= 'A' && $c <= 'Z') {
$flags = $flags + (1 << $j);
}
}
if ($flags <= 25) {
$suffix .= substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ',$flags,1);
}else{
$suffix .= substr('012345', $flags - 26, 1);
}
}
return $inputId . $suffix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment