baker95935

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
$file_path = "test.txt";
if(file_exists($file_path)){
    $file_arr = file($file_path);
    for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
        //echo $file_arr[$i]."<br />";
        $tmpAry[$i]=explode(' ',$file_arr[$i]);
    }
    $count=count($file_arr);
    $offset=rand(0,$count-1);
    $aa=array_slice($tmpAry, $offset,1);
    var_dump($aa);
}
exit;

PHP读取txt文件到数组

加了一个逐行写入

$file_path = "1.txt";
if(file_exists($file_path)){
    $file_arr = file($file_path);
    $handle = fopen("2.txt", "a");
    for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
        $aa=substr($file_arr[$i],0,11)."\r\n";
        
        if ($handle) {
            fwrite($handle, $aa);
            
        }
    }
    fclose($handle);
}
exit;

 

posted on 2018-12-20 13:41  baker95935  阅读(2200)  评论(0编辑  收藏  举报