Base62 PHP Library

repository·2.x·Indexed 41 days ago

https://github.com/tuupola/base62

A PHP library for Base62 encoding and decoding of arbitrary binary data and integers. It supports custom character sets, provides a static proxy via Tuupola\Base62Proxy, and automatically utilizes the GMP extension for performance. Compatible with PHP 7.1+ and PHP 5.6/7.0 via the 1.x branch.

Tokens
1.5K
Snippets
9
Records
9
Agent score
3%

What's inside Base62

  1. Install Base62 via Composer

    2.x

    Install the library using Composer. The current branch requires PHP 7.1 or higher. For older environments (PHP 5.6 or 7.0), you must specify the 1.x branch.

    # For PHP 7.1+
    $ composer require tuupola/base62
    
    # For PHP 5.6 or 7.0
    $ composer require "tuupola/base62:^1.0"
  2. Configure custom character sets

    2.x

    You can customize the Base62 character set by passing a characters key in the constructor options. The library provides two built-in constants:

    • Base62::GMP: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz (Default)
    • Base62::INVERTED: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    use Tuupola\Base62;
    
    // Use the inverted character set
    $inverted = new Base62(["characters" => Base62::INVERTED]);
    print $inverted->encode("Hello world!"); // t8DGCJrgUyuUEwHT
    
    // Use a custom character set
    $custom = new Base62(["characters" => "ABC...[62 unique chars]..."]);
  3. Encode and decode arbitrary data

    2.x

    Use the Tuupola\Base62 class to encode and decode arbitrary binary data (strings). The library automatically uses the GMP extension if available for better performance, falling back to a pure PHP implementation if GMP is missing.

    $base62 = new Tuupola\Base62();
    
    $encoded = $base62->encode(random_bytes(128));
    $decoded = $base62->decode($encoded);
  4. Encode and decode integers

    2.x

    When working specifically with integers, use encodeInteger() and decodeInteger().

    Important: Encoding a string representation of a number (e.g., encode("987654321")) yields a different result than encoding the integer itself (e.g., encodeInteger(987654321)).

    $base62 = new Tuupola\Base62();
    
    $integer = $base62->encodeInteger(987654321); // 14q60P
    print $base62->decodeInteger("14q60P"); // 987654321
    
    // Note the difference:
    $stringResult = $base62->encode("987654321"); // KHc6iHtXW3iD
  5. Use GmpEncoder directly for performance

    2.x

    If you are certain the GMP extension is installed, you can bypass the automatic detection and use Tuupola\Base62\GmpEncoder directly for maximum performance.

    if (function_exists("gmp_init")) {
        $base62 = new Tuupola\Base62\GmpEncoder();
        $encoded = $base62->encode("Hello world!");
    } else {
        /* Handle error... */
    }
  6. Use the Base62 static proxy

    2.x

    If you prefer not to instantiate the class, you can use Tuupola\Base62Proxy to call encoding and decoding methods statically.

    use Tuupola\Base62Proxy as Base62;
    
    $encoded = Base62::encode(random_bytes(128));
    $decoded = Base62::decode($encoded);
    
    $encodedInt = Base62::encodeInteger(987654321);
    $decodedInt = Base62::decodeInteger($encodedInt);
  7. Initialize Base62 with custom options

    2.x

    You can pass an $options array to the Base62 constructor to configure the underlying encoder. This is useful if you need to specify specific character sets or other encoder-specific behaviors.

    use Tuupola\Base62;
    
    $base62 = new Base62([
        // Add configuration options here
    ]);
  8. Use the Base62 class for encoding and decoding

    2.x

    The Tuupola\Base62 class provides a high-level interface for converting data between strings/integers and Base62 representations. It automatically selects the most efficient available encoder (GMP, BCMath, or native PHP) based on your environment.

    Available Methods

    • encode(string $data): string: Encodes a string into a Base62 string.
    • decode(string $data): string: Decodes a Base62 string back into its original string format.
    • encodeInteger(int $data): string: Encodes an integer into a Base62 string.
    • decodeInteger(string $data): int: Decodes a Base62 string back into an integer.
    use Tuupola\Base62;
    
    $base62 = new Base62();
    
    // String encoding/decoding
    $encodedString = $base62->encode('hello');
    $decodedString = $base62->decode($encodedString);
    
    // Integer encoding/decoding
    $encodedInt = $base62->encodeInteger(12345);
    $decodedInt = $base62->decodeInteger($encodedInt);
  9. Base62 Character Sets

    2.x

    The library defines two standard character sets as constants:

    • Base62::GMP: The standard alphanumeric sequence 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.
    • Base62::INVERTED: An inverted alphanumeric sequence 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.
    // Standard alphanumeric
    const GMP = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
    // Inverted alphanumeric
    const INVERTED = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";