PHP读取txt文件到数组


$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;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM