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