要讀取文件里的內(nèi)容,并按逐行進(jìn)行讀取有兩種方式。
1、fgets()逐行讀取文件
fgets() 函數(shù)用于從文件中逐行讀取文件。
注釋:在調(diào)用該函數(shù)之后,文件指針會(huì)移動(dòng)到下一行。
實(shí)例
下面的實(shí)例逐行讀取文件,直到文件末尾為止:
<?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //feof()函數(shù)檢測(cè)是否已到達(dá)文件末尾(EOF)。 while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file); ?>
2、使用PHP file() 函數(shù)
file() 函數(shù)把整個(gè)文件讀入一個(gè)數(shù)組中。
數(shù)組中的每個(gè)元素都是文件中相應(yīng)的一行,包括換行符在內(nèi)。
例如:
<?php $arr = file("test.txt"); print_r($arr); ?>
結(jié)果:
Array ( [0] => Hello World. Testing testing! [1] => Another day, another line. [2] => If the array picks up this line, [3] => then is it a pickup line? )