使用PHP批量生成隨機(jī)用戶名
更新時(shí)間:2008年07月10日 22:56:30 作者:
生成6 ~ 16位的用戶名若干個(gè),主要是文本操作,同事前提是要有一個(gè)字符串包。主要包含三個(gè)程序。
程序一:負(fù)責(zé)從字典中隨機(jī)提取數(shù)據(jù),寫入一個(gè)新文件。(1.php)
<?php
/* 從字典文件中提取隨機(jī)值 */
$file1 = "./Words.dic";
$file2 = "./common_pass_mini.dic";
$file3 = "./Sys_Month_Date.Dic";
$rfile = "./5.dic";
$n = 2000;
//提取字典
$basef = file($file1);
$extf = file($file2);
$extf2 = file($file3);
$bf_sum = (count($basef)-1);
$ef_sum = (count($extf)-1);
$ef2_sum =(count($extf2)-1);
//獲取隨機(jī)用戶名
for ($i=0; $i<$n; $i++)
{
$bn = crand(0, $bf_sum);
$en = crand(0, $ef_sum);
$en2 = crand(0, $ef2_sum);
$name = $basef[$bn]."_".$extf[$en];
$name = str_replace("/r/n", "", $name);
$all_name[] = $name;
}
//寫入文件
$result = implode("/r/n", $all_name);
$fp = fopen($rfile, "a+") or die('Open $rfile failed');
if (fwrite($fp, $result)) {
echo 'Write user succeed!';
} else {
echo 'Write user failed';
}
//生成隨機(jī)數(shù)字函數(shù)
function crand($start, $end)
{
return mt_rand($start, $end);
}
?>
程序二:負(fù)責(zé)把上面生成的數(shù)個(gè)文件的結(jié)果合并。(2.php)
<?php
/* 合并所有生成結(jié)果 jb51.net*/
$result_file = "./result.dic";
$fp = fopen($result_file, "a+") or die("Open $result_file failed");
//合并 1.dic ~ 5.dic
for ($i=1; $i<=5; $i++)
{
$cur_file = file_get_contents($i.".dic");
fwrite($fp, $cur_file);
}
//合并 10.dic ~ 11.dic
for ($i=10; $i<=11; $i++)
{
$cur_file = file_get_contents($i.".dic");
fwrite($fp, $cur_file);
}
fclose($fp);
echo 'Write Succeed';
?>
程序三:負(fù)責(zé)過濾重復(fù)值和不屬于 6~16 之間的值并且生成最終結(jié)果(3.php)
<?php
/* 生成最終結(jié)果 */
$file = "./result.dic";
$target = "./target.dic";
//去掉重復(fù)值
$files = file($file);
$files = array_unique($files);
//判斷值是不是大于6位小于16位
$sum = count($files);
for ($i=0; $i<$sum; $i++)
{
if (strlen($files[$i])>=6 && strlen($files[$i])<=16) {
$rs[] = $files[$i];
} else {
continue;
}
}
//寫入目標(biāo)文件
$result = implode("", $rs);
$fp = fopen($target, "a+") or die("Open $target failed");
fwrite($fp, $result);
echo 'Write succeed';
?>
基本搞定手工,上面生成了 2.7W個(gè)隨機(jī)用戶名,呵呵,保證夠你使用。
復(fù)制代碼 代碼如下:
<?php
/* 從字典文件中提取隨機(jī)值 */
$file1 = "./Words.dic";
$file2 = "./common_pass_mini.dic";
$file3 = "./Sys_Month_Date.Dic";
$rfile = "./5.dic";
$n = 2000;
//提取字典
$basef = file($file1);
$extf = file($file2);
$extf2 = file($file3);
$bf_sum = (count($basef)-1);
$ef_sum = (count($extf)-1);
$ef2_sum =(count($extf2)-1);
//獲取隨機(jī)用戶名
for ($i=0; $i<$n; $i++)
{
$bn = crand(0, $bf_sum);
$en = crand(0, $ef_sum);
$en2 = crand(0, $ef2_sum);
$name = $basef[$bn]."_".$extf[$en];
$name = str_replace("/r/n", "", $name);
$all_name[] = $name;
}
//寫入文件
$result = implode("/r/n", $all_name);
$fp = fopen($rfile, "a+") or die('Open $rfile failed');
if (fwrite($fp, $result)) {
echo 'Write user succeed!';
} else {
echo 'Write user failed';
}
//生成隨機(jī)數(shù)字函數(shù)
function crand($start, $end)
{
return mt_rand($start, $end);
}
?>
程序二:負(fù)責(zé)把上面生成的數(shù)個(gè)文件的結(jié)果合并。(2.php)
復(fù)制代碼 代碼如下:
<?php
/* 合并所有生成結(jié)果 jb51.net*/
$result_file = "./result.dic";
$fp = fopen($result_file, "a+") or die("Open $result_file failed");
//合并 1.dic ~ 5.dic
for ($i=1; $i<=5; $i++)
{
$cur_file = file_get_contents($i.".dic");
fwrite($fp, $cur_file);
}
//合并 10.dic ~ 11.dic
for ($i=10; $i<=11; $i++)
{
$cur_file = file_get_contents($i.".dic");
fwrite($fp, $cur_file);
}
fclose($fp);
echo 'Write Succeed';
?>
程序三:負(fù)責(zé)過濾重復(fù)值和不屬于 6~16 之間的值并且生成最終結(jié)果(3.php)
復(fù)制代碼 代碼如下:
<?php
/* 生成最終結(jié)果 */
$file = "./result.dic";
$target = "./target.dic";
//去掉重復(fù)值
$files = file($file);
$files = array_unique($files);
//判斷值是不是大于6位小于16位
$sum = count($files);
for ($i=0; $i<$sum; $i++)
{
if (strlen($files[$i])>=6 && strlen($files[$i])<=16) {
$rs[] = $files[$i];
} else {
continue;
}
}
//寫入目標(biāo)文件
$result = implode("", $rs);
$fp = fopen($target, "a+") or die("Open $target failed");
fwrite($fp, $result);
echo 'Write succeed';
?>
基本搞定手工,上面生成了 2.7W個(gè)隨機(jī)用戶名,呵呵,保證夠你使用。
您可能感興趣的文章:
- PHP生成隨機(jī)字符串(3種方法)
- PHP生成不重復(fù)隨機(jī)數(shù)的方法匯總
- PHP生成指定長(zhǎng)度隨機(jī)數(shù)最簡(jiǎn)潔的方法
- php隨機(jī)生成數(shù)字字母組合的方法
- PHP生成隨機(jī)用戶名和密碼的實(shí)現(xiàn)代碼
- PHP 生成N個(gè)不重復(fù)的隨機(jī)數(shù)
- php簡(jiǎn)單生成隨機(jī)顏色的方法
- php隨機(jī)輸出名人名言的代碼
- PHP實(shí)現(xiàn)的英文名字全拼隨機(jī)排號(hào)腳本
- PHP基于自定義類隨機(jī)生成姓名的方法示例
- PHP隨機(jī)生成中文段落示例【測(cè)試網(wǎng)站內(nèi)容時(shí)使用】
相關(guān)文章
php中批量刪除Mysql中相同前綴的數(shù)據(jù)表的代碼
Mysql如何批量刪除相同前綴的數(shù)據(jù)表,原理就是讀取數(shù)據(jù)中的所有表,查找class_開頭的表,如果開頭是這個(gè),就刪除。2011-07-07
詳解PHP實(shí)現(xiàn)HTTP服務(wù)器過程
一般來講,PHP很少談到“實(shí)現(xiàn)HTTP服務(wù)”的說法,因?yàn)閺脑缙诘腃GI到后來的PHP-FPM,官方已經(jīng)給出了最穩(wěn)定的HTTP解決方案,你只要配合一個(gè)Apache或Nginx類的服務(wù)器就能實(shí)現(xiàn)穩(wěn)定的HTTP服務(wù)2023-02-02
php提示Failed to write session data錯(cuò)誤的解決方法
這篇文章主要介紹了php提示Failed to write session data錯(cuò)誤的解決方法,較為詳細(xì)的分析了session寫入錯(cuò)誤的原因與解決方法,并附帶說明了php的工作機(jī)制,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
php實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)站在線人數(shù)的方法
這篇文章主要介紹了php實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)站在線人數(shù)的方法,通過獲取服務(wù)器端網(wǎng)絡(luò)參數(shù)及文本文件讀寫實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)的功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
解析PHP函數(shù)array_flip()在重復(fù)數(shù)組元素刪除中的作用
本篇文章是對(duì)PHP函數(shù)array_flip()在重復(fù)數(shù)組元素刪除中的作用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP中使用php5-ffmpeg擷取視頻圖片實(shí)例
這篇文章主要介紹了PHP中使用php5-ffmpeg擷取視頻圖片實(shí)例,本文使用一個(gè)AVI格式視頻為例,講解了如何擷取視頻畫面為圖片,需要的朋友可以參考下2015-01-01
php檢測(cè)數(shù)組長(zhǎng)度函數(shù)sizeof與count用法
這篇文章主要介紹了php檢測(cè)數(shù)組長(zhǎng)度函數(shù)sizeof與count用法,實(shí)例分析了count的用法,并對(duì)sizeof函數(shù)進(jìn)行了詳盡的分析說明,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11

