php产生随机字符串

/**
 * 产生随机字符串
 *
 * @param    int        $length  输出长度
 * @param    string     $chars   可选的 ,默认为 0123456789
 * @return   string     字符串
 */
function random($length, $chars = ‘0123456789‘) {
    $hash = ‘‘;
    $max = strlen($chars) - 1;
    mt_srand();
    for($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}

$a=random(8, ‘1294567890abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ‘);

var_dump($a);///mnt/hgfs/www/test/index.php:28:string ‘otFTNx9u‘ (length=8)
时间: 2024-08-25 02:44:05

php产生随机字符串的相关文章

.net生成随机字符串

生成随机字符串的工具类: /// <summary> /// 随机字符串工具类 /// </summary> public class RandomTools { /// <summary> /// 随机系数 /// </summary> public static int _RandIndex = 0; #region 获取某个区间的一个随机数 /// <summary> /// 获取某个区间的一个随机数 /// </summary>

shell 生成指定范围随机数与随机字符串 .

shell 生成指定范围随机数与随机字符串 分类:             shell              2014-04-22 22:17     20902人阅读     评论(5)     收藏     举报 shellrandomurandomuuidlinux shell 生成指定范围随机数与随机字符串 1.使用系统的 $RANDOM 变量 [plain] view plaincopyprint? [email protected]:~$ echo $RANDOM 17617 [

[PHP]利用openssl_random_pseudo_bytes和base64_encode函数来生成随机字符串

openssl_random_pseudo_bytes函数本身是用来生成指定个数的随机字节,因此在使用它来生成随机字符串时,还需要配合使用函数base64_encode.如下所示: public static function getRandomString($length = 42) { /* * Use OpenSSL (if available) */ if (function_exists('openssl_random_pseudo_bytes')) { $bytes = openss

PHP 生成随机字符串与唯一字符串

说明:生成随机字符串用到的方法有 mt_rand() 生成唯一字符串用到的方法有 md5(),uniqid(),microtime() 代码: 1 <?php 2 /* 3 * 生成随机字符串 4 * @param int $length 生成随机字符串的长度 5 * @param string $char 组成随机字符串的字符串 6 * @return string $string 生成的随机字符串 7 */ 8 function str_rand($length = 32, $char =

随机数+随机字符串

随机数 随机取0-100 arc4random() % 101 随机取80+(0至20) (arc4random() % 21) +80:   随机字符串 -(NSString *)ret32bitString { //随机生成n位字母 int n=4; char data[n]; for (int x=0;x<n;data[x++] = (char)('A' + (arc4random_uniform(26)))); return [[NSString alloc] initWithBytes

PHP学习笔记:万能随机字符串生成函数(已经封装好)

做验证码用到的,然后就把这个函数封装起来,使用时候要设置2个参数: $str设置里要被采集的字符串,比如: $str='efasfgzsrhftjxjxjhsrth'; 则在函数里面生成的字符串就回从efasfgzsrhftjxjxjhsrth里面随机抓取: $codeLen设置要生成的随机字符串,设置5,则生成5个随机字符串. 原理:随机抓取字符串,对字符串进行拼接 效果: 代码: <?php //mt_rand 获取随机数 mt_rand(min, max); $str="abcdef

随机字符串生成

function TfrmPWGenerate.btnGenerateClick(Sender: TObject): string; {max length of generated password}const  intMAX_PW_LEN = 10;var  i: Byte;  s: string;begin  {if you want to use the 'A..Z' characters}  if cbAZ.Checked then    s := 'ABCDEFGHIJKLMNOPQ

创建指定数量的随机字符串

/** * 创建指定数量的随机字符串 * * @param numberFlag * 是否是数字 * @param length * @return String */ private static String createRandom(boolean numberFlag, int length) { String retStr = ""; String strTable = numberFlag ? "1234567890" : "123456789

Android腾讯微博开发之随机字符串与签名实现

Android腾讯微博开发入门之随机字符串与签名实现 直接上代码 1.Utils类,包括签名和随机字符串 import java.util.Random; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; /** * * @author mrlixirong * * 2011-11-16 */ public class Utils { //签名 public static String getSignature(St

高并发下获取随机字符串

#region 获取随机字符串 //digit 最终返回的字符串的长度 public static string BuildCode(int digit) { StringBuilder resultCode = new StringBuilder(); Random ran = new Random(GetRandomSeed()); for (int i = 0; i < digit; i++) { resultCode.Append(("0123456789").Subst