ShortCode generator for PHP. Create short, hash like codes. Codes can be random or reversible. Output format is customizable (see the list below).
You can generate random string code of your desired character length, e.g. 4, 6, 8 ... up to 20.
Also, you can generate reversible codes from numbers. It's useful when you'll need to trace the original number from a reference code string.
Can be used for generating small reference codes, tiny URLs or any other purpose.
Install the latest version with
$ composer require ajaxray/short-code
ShortCodeCode::FORMAT_ALNUM
: (Default) Alphanumaric characters. includes 0-9, a-z and A-ZShortCodeCode::FORMAT_ALNUM_CAPITAL
: Alphanumaric characters. includes 0-9 and A-ZShortCodeCode::FORMAT_ALNUM_SMALL
: Alphanumaric characters. includes 0-9 and a-zShortCodeCode::FORMAT_CHAR_CAPITAL
: Capital letter characters. includes only A-ZShortCodeCode::FORMAT_CHAR_SMALL
: Small letter characters. includes only a-zShortCodeCode::FORMAT_NUMBER
: Numbers. includes only 0-9. Can be used for random number generation<?php
ShortCodeRandom::get();
// Something like (8 chars by default) : aWg2m5Q3
ShortCodeRandom::get(6);
// 6 character length. e.g. r43Nx2
ShortCodeRandom::get(8, ShortCodeCode::FORMAT_ALNUM_SMALL);
// 8 characters with alnum (small letter only). e.g. f43nbg3e2
<?php
ShortCodeReversible::convert(46345223);
// Output: 38svB
ShortCodeReversible::revert('38svB');
// Output: 46345223
// If you specify a format for converting, remember to use the same format for reverting
ShortCodeReversible::convert(46345223, ShortCodeCode::FORMAT_ALNUM_CAPITAL);
// Output: RLC7B
ShortCodeReversible::revert('RLC7B', ShortCodeCode::FORMAT_ALNUM_CAPITAL);
// Output: 46345223
You can create code with a minimum output length. You have to mention the length for both, converting and reverting functions.
<?php
// Creating alphanumeric code with minimum 6 character and reverting
ShortCodeReversible::convert(9876, ShortCodeCode::FORMAT_ALNUM, 6);
// Output: 1002zi
ShortCodeReversible::revert('1002zi', ShortCodeCode::FORMAT_ALNUM, 6);
// Output: 9876
// Creating small letter only code with minimum 6 character and reverting
ShortCodeReversible::convert(9876, ShortCodeCode::FORMAT_CHAR_SMALL, 6);
// Output: baardm
ShortCodeReversible::revert('baardm', ShortCodeCode::FORMAT_CHAR_SMALL, 6);
// Output: 9876
Please note that, using this method, you can only ensure the minimum character length. This length may exceed for larger numbers.
This library has dependency on BCMath PHP extension. Please check the installation page in case you don't have it configured.
Also it can be installed on linux based os with something similar to -
// FOr php 5.x
sudo apt-get install php-bcmath
// For php 7
sudo apt-get install php7.0-bcmath
"This is the Book about which there is no doubt, a guidance for those conscious of Allah" - Al-Quran