For an ecommerce site I want to generate a random coupon code that looks better than a randomly generated value. It should be a readable coupon code, all in uppercase with no special characters, only letters (A-Z) and numbers (0-9).
Since people might be reading this out / printing it elsewhere, we need to make this a simple-to-communicate value as well, perhaps 8-10 characters long.
Something like perhaps,
AHS3DJ6BW
B83JS1HSK
(I typed that, so it's not really that random)
http://webarto.com/35/php-random-string-generator
Here you go.
Why don't keep it simple?
Always returns 13 character long uppercased random code.
You can optimize this by preallocating the
$res
string and caching the result ofstrlen($chars)-1
. This is left as an exercise to the reader, since probably you won't be generating thousands of coupons per second.Try this:
You can use the coupon code generator PHP class file to generate N number of coupons and its customizable, with various options of adding own mask with own prefix and suffix. Simple PHP coupon code generator
Example:
coupon::generate(8); // J5BST6NQ