Like in a lot of companies, SalesForce is used to manage customer file and information.

Our application need to interface with the SF Database to either update/create records or fetch some information for our support/marketing.

We encounter a weird issue where we started to have problems with our salesforce ID not being recognized correctly with their API. After some research I discovered that SalesForce use 2 types of ids.

The first one is a 15 case-sensitive ID and the other an 18 case-insensitive ID. From their documentation, the 18 chars one is the new standard. I had to find how to generate the 18 from the 15.

I found a code done in APEX that I translated in PHP. I hope it will be as useful to me as it is to you.

<?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;
}