您好,欢迎访问一九零五行业门户网

生成任意长度字符串的类(自由定制)

可定制长度、字母、数字、大小写 /* * 生成随机字符串的类,默认只包含数字、大小写字母 * @author jerry */class randomstring { /* * 生成的字符串包含的字符设置 */ const numeric_only = 1; //只含有数字 const letter_only = 2; //只含有字母 const mixed = 3; //混合数字和字母 /* * 用户传入变量,分别为字符串长度;包含的字母;是否包含大写字母 */ protected $length, $type, $upper; /* * 参数初始化 * @param int,$length 字符串长度 * @param const,$type 生成字符串的类型 * @param boolean,$upper 是否含有大写字母 */ public function __construct($length = 16, $type = self::mixed, $upper = true) { $this->length = $length; $this->type = $type; $this->upper = $upper; } /* * 对象被转化为字符串时调用 * @return string */ public function __tostring() { return $this->pickupchars(); } /* * 生成随机字符串 * @global $type * @return string,$string */ public function pickupchars() { switch ($this->type) { case self::numeric_only: $raw = '0123456789'; break; case self::letter_only: $raw = 'qwertyuioplkjhgfdsazxcvbnm' . 'qwertyuioplkjhgfdsazxcvbnm'; break; default: $raw = 'qwertyuioplkjhgfdsazxcvbnm' . 'qwertyuioplkjhgfdsazxcvbnm' . '0123456789'; break; } $string = ''; for ($index = 0; $index length; $index++) $string .= substr($raw, mt_rand(0, strlen($raw) - 1), 1); if (!$this->upper) $string = strtolower($string); return $string; }}//echo new randomstring(170, randomstring::mixed, true).'
';
复制代码
其它类似信息

推荐信息