Created
May 23, 2020 12:23
-
-
Save olivsinz/2a2e39d122d2f6a296c4948b9466602c to your computer and use it in GitHub Desktop.
PHP Script to Create a unique alphanumeric string like Voucher Code, Referral Code, Coupon Code, OTP or any kind of Promotional Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
*/ | |
trait Uniquable | |
{ | |
// this function can create a unique alphanumeric string. To create a | |
// unique string contains letter and number (alphanumeric) with the character limit $limit. These kinds of | |
// unique string can be used as Voucher Code, Referral Code, Coupon Code, OTP or any kind of Promotional Code. | |
// | |
// In the above function, I’m using the default PHP functions. | |
// base_convert – Convert a number between arbitrary bases. | |
// sha1 – Calculate the sha1 hash of a string. | |
// uniqid – Generate a unique ID. | |
// mt_rand – Generate a random value via the Mersenne Twister Random Number Generator. | |
function unique_code($limit) | |
{ | |
return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment