HashID is a lightweight PHP class for encoding and decoding integers to and from short, URL-safe alphanumeric strings using a Base36 character set (0-9, a-z). It is designed to be fast, thread-safe, and case-insensitive (optional), making it ideal for generating compact identifiers for URLs, database keys, or other applications where short, readable strings are needed.
- Fast and Efficient: Uses a lookup table for quick encoding and decoding.
- Case-Insensitive Decoding: Optionally ignores case for decoding (e.g.,
kf12oiandKF12OIare treated the same). - Ignores Non-Base36 Characters: Strips invalid characters during decoding for robust handling.
- Thread-Safe: Static methods ensure safe usage in multi-threaded environments.
- No External Dependencies: Pure PHP implementation, easy to integrate.
- MIT License: Freely usable and modifiable.
- Add the package to your project:
composer require halityesil/hashid
- Include the class in your PHP code:
require 'vendor/autoload.php'; use HashID;
- Download
HashID.phpfrom the GitHub repository. - Include it in your project:
require_once 'path/to/HashID.php';
Convert a number to a Base36 string:
$hash = HashID::encode(1234567890);
echo $hash; // Outputs: "kf12oi"Convert a Base36 string back to the original number:
$number = HashID::decode('kf12oi');
echo $number; // Outputs: 1234567890By default, decoding is case-insensitive. You can explicitly control this behavior:
$number = HashID::decode('KF12OI', true); // Case-insensitive
echo $number; // Outputs: 1234567890
$number = HashID::decode('KF12OI', false); // Case-sensitive, throws exception if invalid- Negative numbers in
encodethrow anInvalidArgumentException. - Non-Base36 characters in
decodeare ignored. - Empty strings in
decodereturn0.
try {
$hash = HashID::encode(4522);
echo $hash; // Outputs: "c6"
$number = HashID::decode('c6');
echo $number; // Outputs: 4522
$number = HashID::decode('C6', true); // Case-insensitive
echo $number; // Outputs: 4522
$number = HashID::decode('c6!@#', true); // Ignores invalid characters
echo $number; // Outputs: 4522
} catch (\InvalidArgumentException $e) {
echo "Error: " . $e->getMessage();
}- Compact URLs: Generate short, readable identifiers for URLs (e.g.,
/product/c6instead of/product/4522). - Performance: Lookup table ensures fast encoding/decoding.
- Robustness: Handles edge cases like empty strings, invalid characters, and case sensitivity.
- No Dependencies: Lightweight and easy to integrate into any PHP project.
- Description: Encodes an integer into a Base36 string.
- Parameters:
$number: The integer to encode (must be non-negative).
- Returns: The Base36-encoded string.
- Throws:
\InvalidArgumentExceptionif the input is negative.
- Description: Decodes a Base36 string back to the original integer.
- Parameters:
$hash: The Base36 string to decode.$insensitive: Iftrue, decoding is case-insensitive (default:true).
- Returns: The decoded integer.
- Throws:
\InvalidArgumentExceptionif case-sensitive decoding encounters invalid characters.
- Encoding: O(log n) where n is the input number.
- Decoding: O(m) where m is the length of the input string.
- Optimized with a lookup table for fast character-to-index mapping.
- Uses a simple Base36 alphabet (
0-9,a-z), making it predictable but safe for most use cases. - For applications requiring obfuscation, consider adding a salt (not implemented in this version but can be extended).
- Does not expose sensitive data, as it only converts integers to strings.
Contributions are welcome! Please submit issues or pull requests to the GitHub repository.
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m 'Add your feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
This project is licensed under the MIT License.
- Halit Yeşil (@halityesil) (halityesil.com) (x.com/halityesil)
- 1.0.0 (Released: 2025-07-10)