用于生成外观漂亮的优惠券代码的 PHP 代码(字母和数字的混合)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3521621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP code for generating decent-looking coupon codes (mix of letters and numbers)
提问by QAQuest
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).
对于电子商务网站,我想生成一个看起来比随机生成的值更好的随机优惠券代码。它应该是一个可读的优惠券代码,全部大写,没有特殊字符,只有字母(AZ)和数字(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.
由于人们可能会在其他地方阅读/打印它,因此我们也需要将其设为易于交流的值,长度可能为 8-10 个字符。
Something like perhaps,
也许像这样的东西,
AHS3DJ6BW
B83JS1HSK
(I typed that, so it's not really that random)
(我输入的,所以它不是真的那么随机)
回答by Artefacto
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "";
for ($i = 0; $i < 10; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
You can optimize this by preallocating the $res
string and caching the result of strlen($chars)-1
. This is left as an exercise to the reader, since probably you won't be generating thousands of coupons per second.
您可以通过预分配$res
字符串并缓存strlen($chars)-1
. 这留给读者作为练习,因为您可能不会每秒生成数千张优惠券。
回答by Gumbo
Try this:
尝试这个:
substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 10)
回答by joashp
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
您可以使用优惠券代码生成器PHP类文件生成N个优惠券及其可定制的,具有添加自己的前缀和后缀的掩码的各种选项。简单的 PHP 优惠券代码生成器
Example:
coupon::generate(8); // J5BST6NQ
例子:
coupon::generate(8); // J5BST6NQ
回答by Otar
Why don't keep it simple?
为什么不保持简单?
<?php
echo strtoupper(uniqid());
?>
Always returns 13 character long uppercased random code.
始终返回 13 个字符长的大写随机代码。
回答by Lèse majesté
If there are no security requirements for these, then you don't really need randomly generated codes. I would just use incremental IDs, such as those generated by whatever RDBMS you use. Optionally, if you have different types of coupons, you could prefix the codes with something, e.g.:
如果这些没有安全要求,那么你真的不需要随机生成的代码。我只会使用增量 ID,例如由您使用的任何 RDBMS 生成的 ID。或者,如果您有不同类型的优惠券,您可以在代码前加上一些东西,例如:
CX00019 QZ0001C
CX0001A QZ0001D
CX0001B QZ0001E
Alternately, you could even use dictionary words in the coupon, as such coupon codes are easier to remember and faster for users to type. Companies like Dreamhost use these for their promo codes, e.g.:
或者,您甚至可以在优惠券中使用字典词,因为这样的优惠券代码更容易记住,用户输入也更快。Dreamhost 等公司将这些用于他们的促销代码,例如:
Promo60
NoSetupFee
YELLOWGORILLA82
Some of these are obviously human-created (which you might want to have the option of), but they can also be generated using a dictionary list. But even if they are randomly-generated nonsense phrases, the fact that the characters follow a logical pattern still makes it much more user-friendly than something like R7QZ8A92F1
. So I would strongly advise against using the latter type of coupon codes just on the basis that they "look cool". Your customers will thank you.
其中一些显然是人工创建的(您可能希望选择),但它们也可以使用字典列表生成。但即使它们是随机生成的无意义短语,字符遵循逻辑模式的事实仍然使它比诸如R7QZ8A92F1
. 所以我强烈建议不要使用后一种优惠券代码,因为它们“看起来很酷”。您的客户会感谢您。
回答by AdRock
$size = 12;
$string = strtoupper(substr(md5(time().rand(10000,99999)), 0, $size));
回答by Dejan Marjanovic
http://webarto.com/35/php-random-string-generator
http://webarto.com/35/php-random-string-generator
Here you go.
干得好。
function randr($j = 8){
$string = "";
for($i=0;$i < $j;$i++){
srand((double)microtime()*1234567);
$x = mt_rand(0,2);
switch($x){
case 0:$string.= chr(mt_rand(97,122));break;
case 1:$string.= chr(mt_rand(65,90));break;
case 2:$string.= chr(mt_rand(48,57));break;
}
}
return strtoupper($string); //to uppercase
}
回答by tamasd
function generateCouponCode($length = 8) {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
for($i = 0; $i < $length; ++$i) {
$random = str_shuffle($chars);
$ret .= $random[0];
}
return $ret;
}
回答by Haim Evgi
you can find a lot of function in php rand manual
http://php.net/manual/en/function.rand.php
您可以在 php rand 手册http://php.net/manual/en/function.rand.php 中找到很多功能
i like this one
我喜欢这个
<?php
//To Pull 8 Unique Random Values Out Of AlphaNumeric
//removed number 0, capital o, number 1 and small L
//Total: keys = 32, elements = 33
$characters = array(
"A","B","C","D","E","F","G","H","J","K","L","M",
"N","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9");
//make an "empty container" or array for our keys
$keys = array();
//first count of $keys is empty so "1", remaining count is 1-7 = total 8 times
while(count($keys) < 8) {
//"0" because we use this to FIND ARRAY KEYS which has a 0 value
//"-1" because were only concerned of number of keys which is 32 not 33
//count($characters) = 33
$x = mt_rand(0, count($characters)-1);
if(!in_array($x, $keys)) {
$keys[] = $x;
}
}
foreach($keys as $key){
$random_chars .= $characters[$key];
}
echo $random_chars;
?>
回答by palmic
$length = 9;
$code = (strtoupper(substr(md5(time()), 0, $length)));