Home  >  Article  >  Backend Development  >  How to read txt file line by line in php and write it into an array Original, _PHP Tutorial

How to read txt file line by line in php and write it into an array Original, _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:47:241218browse

How php reads a txt file line by line and writes it into an array. Original,

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1026047.htmlTechArticleThe method of php reading txt file line by line and writing into array is original. The example of this article tells about php reading txt line by line Method for writing files into arrays. Share it with everyone for your reference. The details are as follows: Suppose there is...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn