Home >Backend Development >PHP Tutorial >How to read txt file line by line in php and write it into an array Original, _PHP Tutorial
The example in this article describes how php reads a txt file line by line and writes it into an array. Share it with everyone for your reference. The details are as follows:
Suppose there is a user.txt file as follows:
user01 user02 user03 user04 user05 user06 user07 user08 user09 user10 user11 user12
The method of reading user.txt line by line and writing it into an array is as follows:
$file = fopen("username.txt", "r"); $user=array(); $i=0; //输出文本中所有的行,直到文件结束为止。 while(! feof($file)) { $user[$i]= fgets($file);//fgets()函数从文件指针中读取一行 $i++; } fclose($file); $user=array_filter($user); print_r($user);
Problem solved!
I hope this article will be helpful to everyone’s PHP programming design.