RandEx.php 673 B

123456789101112131415161718192021222324
  1. <?php
  2. class RandEx {
  3. public function random($length=6, $type='string', $convert=0){
  4. $config = array(
  5. 'number'=>'1234567890',
  6. 'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  7. 'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
  8. 'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  9. );
  10. if(!isset($config[$type])) $type = 'string';
  11. $string = $config[$type];
  12. $code = '';
  13. $strlen = strlen($string) -1;
  14. for($i = 0; $i < $length; $i++){
  15. $code .= $string{mt_rand(0, $strlen)};
  16. }
  17. if(!empty($convert)){
  18. $code = ($convert > 0)? strtoupper($code) : strtolower($code);
  19. }
  20. return $code;
  21. }
  22. }