在讀取文本時,咱們要注意一個事情,那就是換行符,應爲咱們在寫文檔時會手動換行,這個換行符需不須要保存就要看本身的需求了。ide
這裏封裝了兩個方法,一個保留換行,一個不保留。$path爲文件路徑+文件名 函數
1.不保留換行 spa
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 function read($path){ 2 $file = fopen($path, "r"); 3 $user=array(); 4 $i=0; 5 //輸出文本中全部的行,直到文件結束爲止。 6 while(! feof($file)) 7 { 8 $user[$i]= trim(fgets($file));//fgets()函數從文件指針中讀取一行 9 $i++; 10 } 11 fclose($file); 12 $user=array_filter($user); 13 return $user; 14 }
2.保留換行指針
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 function read($path){ 2 $file = fopen($path, "r"); 3 $user=array(); 4 $i=0; 5 //輸出文本中全部的行,直到文件結束爲止。 6 while(! feof($file)) 7 { 8 $user[$i]= fgets($file);//fgets()函數從文件指針中讀取一行 9 $i++; 10 } 11 fclose($file); 12 $user=array_filter($user); 13 return $user; 14 }