Home >Backend Development >PHP Tutorial >PHP program for batch generating random usernames

PHP program for batch generating random usernames

WBOY
WBOYOriginal
2016-07-25 09:04:19978browse
  1. /* Extract random values ​​from dictionary file*/

  2. $file1 = "./Words.dic";
  3. $file2 = "./common_pass_mini.dic";
  4. $file3 = "./Sys_Month_Date.Dic";
  5. $rfile = "./5.dic";
  6. $n = 2000;

  7. //Extract dictionary

  8. $basef = file( $file1);
  9. $extf = file($file2);
  10. $extf2 = file($file3);
  11. $bf_sum = (count($basef)-1);
  12. $ef_sum = (count($extf)-1 );
  13. $ef2_sum =(count($extf2)-1);

  14. //Get a random username

  15. for ($i=0; $i<$n; $i++)
  16. {
  17. $bn = crand(0, $bf_sum);
  18. $en = crand(0, $ef_sum);
  19. $en2 = crand(0, $ef2_sum);
  20. $name = $basef[$bn]."_ ".$extf[$en];
  21. $name = str_replace("/r/n", "", $name);
  22. $all_name[] = $name;
  23. }

  24. //Write file

  25. $result = implode("/r/n", $all_name);
  26. $fp = fopen($rfile, "a+") or die('Open $rfile failed');
  27. if (fwrite ($fp, $result)) {
  28. echo 'Write user succeed!';
  29. } else {
  30. echo 'Write user failed';
  31. }

  32. //Generate random number function

  33. function crand($start, $end)
  34. {
  35. return mt_rand($start, $end);
  36. }
  37. ?>

Copy the code

2. Merge the several files generated above result

  1. /* Merge all generated results jbxue.com*/

  2. $result_file = "./result.dic";< /p>

  3. $fp = fopen($result_file, "a+") or die("Open $result_file failed");

  4. //Merge 1.dic ~ 5. dic

  5. for ($i=1; $i<=5; $i++)
  6. {
  7. $cur_file = file_get_contents($i.".dic");
  8. fwrite($fp, $cur_file);
  9. }
  10. //Merge 10.dic ~ 11.dic

  11. for ($i=10; $i<=11; $i++)
  12. {
  13. $cur_file = file_get_contents($i.".dic") ;
  14. fwrite($fp, $cur_file);
  15. }
  16. fclose($fp);
  17. echo 'Write Succeed';
  18. ?>

Copy code

3. Filter duplicate values and values ​​that do not fall between 6 and 16, and generate final usable data

  1. /* Generate final result*/

  2. $file = "./result.dic";

  3. $target = ". /target.dic";

  4. //Remove duplicate values ​​

  5. $files = file($file);
  6. $files = array_unique($files);

  7. $sum = count($files);
  8. for ($i=0; $i<$sum; $i++)
  9. {
  10. if (strlen($files[ $i])>=6 && strlen($files[$i])<=16) {
  11. $rs[] = $files[$i];
  12. } else {
  13. continue;
  14. }
  15. }< /p>
  16. //Write the target file

  17. $result = implode("", $rs);
  18. $fp = fopen($target, "a+") or die("Open $target failed") ;
  19. fwrite($fp, $result);
  20. echo 'Write succeed';
  21. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn