PHP 讀取與寫入txt檔案並並賦值給陣列

2021-06-19 06:00:27 字數 1096 閱讀 1817

php寫入txt檔案換行

今天遇到了乙個問題就是用fwrite寫入txt檔案的時候用 \r\n不能換行

試了很久都沒找到辦法!

突然之間想到乙個東東以前看書見過後來還是用這個東東給解決了!

現在寫出來預防將來忘記也希望能幫到需要幫助的朋友!

直接看**:

<?php

$stream = fopen("robots.txt", "w+");

fwrite($stream, "你換行了嗎?\r\n我已經換行了!");

?>

注意:**裡面不要用單引號,我就是錯在這步上如果用單引號輸出的將是user-agent: *\r\ndisallow: / 因為php裡面的單引號是不對內容裡面的東西進行替換的所以用雙引號,因為雙引號php是檢查裡面的東西的!

本人表達能力不好! 沒辦法湊合著看吧!

完!

php讀取txt檔案的內容並賦值給陣列

<?php

$id1="$id1=0001"."\r\n";

$id2="$id2=0002"."\r\n";

$id3="$id3=0003"."\r\n";

$id4="$id4=0004"."\r\n";

//write

//**裡面不要用單引號

$fp = fopen("aa.txt", "w+");

flock($fp, lock_ex);

fwrite($fp, $id1);

fwrite($fp, $id2);

fwrite($fp, $id3);

fwrite($fp, $id4);

flock($fp, lock_un);

fclose($fp);

//方法1

$file = "aa.txt";

$content = file_get_contents($file);

//echo $content;

$array = explode("\r\n", $content);

print_r($array);

//echo $array[0];

for($i=0; $i

PHP寫入txt檔案

myfiletoread fopen txt readme.txt r or die unable to open file myfileread fread myfiletoread,20 myfiletowrite fopen txt writeme.txt a or die unable to...

python 寫入 讀取txt檔案

with open desc.txt w as f f.write 我是個有想法的小公舉 這句 自帶檔案關閉功能。比較常用的檔案讀寫選項 r 以讀的方式開啟,只能讀檔案,若檔案不存在,則發生異常 w 以寫的方式開啟,只能寫檔案,如果檔案不存在,建立該檔案 如果檔案已存在,先清空,再開啟檔案 rb 以...

PHP讀取TXT檔案寫入資料庫

php讀取txt檔案寫入資料庫完整 如下 包括檔案型別 大小 欄位為空的判斷,以及事務的處理 uploadfile upload files basename files userfile name message 判斷上傳檔案格式 string txt extend pathinfo files ...