2015年8月16日日曜日

ランダム文字列 その2

ランダム文字列 その2


function mf_rand()で作成した文字列は重複することがよくあります。
今回の変更は、function mf_rand()は文字列のArrayを作成することに
し、データーベースの比較して、重複する場合、文字列の順番を変える
ことにしました。


<?php
set_time_limit(0);

$db = new sqlite3('xtext.db');
$db->busyTimeout(10000);

for ($i=0; $i<3000; $i++){
//$temp_id = uniqid(mf_rand());
//$temp_id = uniqid();
$temp_id = mf_rand();
do{
shuffle($temp_id);
$temp_id_str = implode($temp_id);
$id_check = $db->querySingle(sprintf("select * from title where url_id='%s'",$temp_id_str),true);
if($id_check){
//print_r ( $id_check);
}
}while($id_check);
$insert = sprintf("insert into title(url_id)VALUES('%s')",$temp_id_str);
$db->exec($insert);
}
$db->close();

function mf_rand(){
$str = array(range('z','a'),range('Z','A'),range('9','0'));
$length = 11;
$str_r =array();
for ($i=0; $i<$length ;$i++){
$temp = $str[array_rand($str)];
$str_r[count($str_r)] = $temp[array_rand($temp)];
}
return($str_r);
}
?>

0 件のコメント: