Home  >  Article  >  php教程  >  php逐行读取txt文件写入数组的方法 原创,

php逐行读取txt文件写入数组的方法 原创,

WBOY
WBOYOriginal
2016-06-13 08:58:501174browse

php逐行读取txt文件写入数组的方法 原创,

本文实例讲述了php逐行读取txt文件写入数组的方法。分享给大家供大家参考。具体如下:

假设有user.txt文件如下:

user01
user02
user03
user04
user05
user06
user07
user08
user09
user10
user11
user12

逐行读取user.txt并写入数组的方法如下:

$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);

问题搞定!

希望本文所述对大家的php程序设计有所帮助。

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