PHP code for generating decent-looking coupon codes (mix of letters and numbers)

Viewed 57703

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)

11 Answers

Just Write

 $voucher_no =  date('ymd') . rand(1000, 9999);
 while(SapItem::where('voucher_no', $voucher_no)->exists()){
  $voucher_no = date('ymd') . rand(1000, 9999);
 }

Output: 2204171447

Related