如何用PHP保存不同文本文件中的随机数

时间:2013-09-10 20:47:06

标签: php

我在这里只需要一些脚本帮助 - 我想在目录下的不同文本文件中保存每100个兰特号码 - 所以每次我使用循环来生成每100个随机数的兰特数我想要保存它们不同的文本文件,所以这是如何工作,但我不知道如何将不同的文本文件中的每个100兰特号保存为不存在

<?php  

    function randz()
    {
        $file_path = $_SERVER['DOCUMENT_ROOT'] . "testing/files/";
        $content = NULL;
        $start = 1111111;
        $ofset = 9999999;
        $counter = 1;

        for($i=1; $i <= 500; $i++)
        {
            if( $i == 100 )
             {
                $rand = mt_rand($start, $ofset) * 999999;
                $cut  = substr($rand, 0,7);
                $content .= $i .'-'. $cut."\r\n";
                $file_path =  $_SERVER['DOCUMENT_ROOT'] . "testing/files/"."text_".$counter++."_".$counter++.".txt";
                continue;
             }
        }

        $fp = fopen( $file_path , "wb" );
        fwrite($fp,$content);
        fclose($fp);

    }

    randz();

?>

3 个答案:

答案 0 :(得分:0)

我质疑你现有的很多代码。因此,我将回答这个问题:

  

如何使用php

在不同的txt文件中保存rand数字

看看file_put_contents()

一个简单的例子:

for ($i = 0; $i < 100; ++$i) {
   file_put_contents("{$i}.txt", rand());
}

答案 1 :(得分:0)

尝试:

function randz($fileCount){
  $file_path = $_SERVER['DOCUMENT_ROOT'].'testing/files/text_';
  $content ='';
  for($i=1,$l=$fileCount+1; $i<$l; $i++){
    for($n=1; $n<101; $n++){
      $rand = (string)(mt_rand(1111111, 9999999) * 999999);
      $cut = substr($rand, 0,7);
      $content .= $n.'-'.$cut."\r\n";
    }
    file_put_contents($file_path.$i.'.txt', $content);
  }
}
// create 10 files width 100 random content lines
randz(10);

答案 2 :(得分:0)

如果可能的话,我会先对你的代码做一些评论:

<?php  

    function randz()
    {
        $file_path = $_SERVER['DOCUMENT_ROOT'] . "testing/files/";
        $content = NULL; // You are appending strings, 
                         // so in order to avoid a type cast, you'd prefer:
        $content = "";
        $start = 1111111;
        $ofset = 9999999;
        $counter = 1;

        for($i=1; $i <= 500; $i++)
        {
            // Why a loop and then an if with == ?
            // Maybe you wanted to use if( ($i % 100) == 0 ) ?
            if( $i == 100 )
             {
                $rand = mt_rand($start, $ofset) * 999999;
                $cut  = substr($rand, 0,7);
                $content .= $i .'-'. $cut."\r\n";
                $file_path =  $_SERVER['DOCUMENT_ROOT'] . "testing/files/"."text_".$counter++."_".$counter++.".txt";
                // Never pre/post increment twice the same
                // variable in the same line.
                // Does that increment counter in 1, or two?
                continue;
             }
        }

        $fp = fopen( $file_path , "wb" );
        fwrite($fp,$content);
        fclose($fp);

    }

    randz();

?>

为什么不呢,

<?php  

    function randz($file_number)
    {
        $file_path = $_SERVER['DOCUMENT_ROOT'] . "testing/files/";
        $content = array();
        $start = 1111111;
        $ofset = 9999999;
        $counter = 1;

        for($i = 0; i < 100; i++)
        {
            $rand = mt_rand($start, $ofset) * 999999;
            $cut = substr($rand, 0, 7);
            $content[] = $i . "-" . $cut;
        }

        $str_content = implode("\r\n", $content);
        file_put_contents($file_path ."text_" . $filenumber . ".txt", $str_content);
    }

    for($i = 0; $i < 500; $i++)
    {
        randz($i);
    }

?>