PHP code example of nabeghe / alphanum

1. Go to this page and download the library: Download nabeghe/alphanum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nabeghe / alphanum example snippets


use Nabeghe\Alphanum\Alphanum;

// Encoding
echo Alphanum::generate(0);            // 0
echo Alphanum::generate(1);            // 1
echo Alphanum::generate(10);           // A
echo Alphanum::generate(24);           // O
echo Alphanum::generate(62);           // _
echo Alphanum::generate(63);           // 10
echo Alphanum::generate(100);          // 1b

// Decoding
echo Alphanum::toDecimal('0');         // 0
echo Alphanum::toDecimal('1');         // 1
echo Alphanum::toDecimal('A');         // 10
echo Alphanum::toDecimal('O');         // 24
echo Alphanum::toDecimal('_');         // 62
echo Alphanum::toDecimal('10');        // 63
echo Alphanum::toDecimal('1b');        // 100

use Nabeghe\Alphanum\Alphanum;

// Base60 Sexagesimal
$encoded60 = Alphanum::generate(1403, Alphanum::BASE60); // NN
$decoded60 = Alphanum::toDecimal('NN', Alphanum::BASE60); // 1403

// Base62 (0-9, A-Z, a-z)
$encoded62 = Alphanum::generate(123456789, Alphanum::BASE62);
$decoded62 = Alphanum::toDecimal($encoded62, Alphanum::BASE62);

// Base58 (Bitcoin style: excludes 0, O, I, l to prevent visual confusion)
$encoded58 = Alphanum::generate(987654321, Alphanum::BASE58);
$decoded58 = Alphanum::toDecimal($encoded58, Alphanum::BASE58);

// Base36 (0-9, a-z - case insensitive)
$encoded36 = Alphanum::generate(1000000, Alphanum::BASE36);
$decoded36 = Alphanum::toDecimal($encoded36, Alphanum::BASE36);

// Custom alphabet (e.g. Hexadecimal or custom shuffled charset)
$customAlphabet = '0123456789abcdef';
$hex = Alphanum::generate(255, $customAlphabet); // ff

use Nabeghe\Alphanum\Alphanum;

$bigId = '123456789012345678901234567890';

// Encode large decimal string
$shortCode = Alphanum::generate($bigId);

// Decode back to arbitrary-precision decimal string
$restoredBigId = Alphanum::toBigDecimal($shortCode);
echo $restoredBigId; // '123456789012345678901234567890'

// Auto-decoder: returns int if it fits in PHP_INT_MAX, or string if it exceeds
$val = Alphanum::decode($shortCode);

use Nabeghe\Alphanum\Alphanum;

// Pad to at least 6 characters (padded with '0' by default)
echo Alphanum::generate(5, null, 6); // '000005'

// Custom pad character
echo Alphanum::generate(10, null, 6, 'X'); // 'XXXXXA'

// Manual pad / unpad helpers
echo Alphanum::pad('1f', 6, '0');    // '00001f'
echo Alphanum::unpad('00001f', '0'); // '1f'

use Nabeghe\Alphanum\Alphanum;

Alphanum::isValid('1f');                     // true
Alphanum::isValid('1f!@#');                  // false
Alphanum::isValid('zO', Alphanum::BASE62);   // true

use Nabeghe\Alphanum\Alphanum;

$shortener = new Alphanum(Alphanum::BASE58, 6, '1');

$code = $shortener->encodeNumber(42);  // '11111k'
$num  = $shortener->decodeToInt($code); // 42

$isValid = $shortener->validate($code); // true
echo $shortener->getBase();             // 58