php逐行读取txt文件写入数组的方法[原创]_php技巧

WBOY
Release: 2016-05-16 20:12:00
Original
1604 people have browsed it

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

假设有user.txt文件如下:

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

Copy after login

逐行读取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);

Copy after login

问题搞定!

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template