Home > Backend Development > PHP Tutorial > How to get the number of lines in a file in PHP_PHP Tutorial

How to get the number of lines in a file in PHP_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:51:18
Original
829 people have browsed it

How to get the number of lines in a file in PHP

provides two implementation methods. Although the second one is simple and easy to understand, the first one is the most efficient

First type:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$file_path = 'xxx.txt'; //文件路径

$line = 0 ; //初始化行数

//打开文件

$fp = fopen($file_path , 'r') or die("open file failure!");

if($fp){

//获取文件的一行内容,注意:需要php5才支持该函数;

while(stream_get_line($fp,8192,"n")){

$line ;

}

fclose($fp);//关闭文件

}

//输出行数;

echo $line;

?>

1 2

3

4

5

1

2

3

4

$line = count(file('filename'));

echo $line;

?>

6 7

8 9

1011 12 13 14 15
$file_path = 'xxx.txt'; //File path
$line = 0; //Initialize the number of lines<🎜> <🎜>//Open file<🎜> <🎜>$fp = fopen($file_path , 'r') or die("open file failure!");<🎜> <🎜>if($fp){<🎜> <🎜>//Get a line of content from the file. Note: PHP5 is required to support this function; <🎜> <🎜>while(stream_get_line($fp,8192,"n")){<🎜> <🎜>$line ;<🎜> <🎜>}<🎜> <🎜>fclose($fp);//Close the file<🎜> <🎜>}<🎜> <🎜>//Output the number of lines; <🎜> <🎜>echo $line;<🎜> <🎜>?>
Second type:
1 2 3 4 <🎜>$line = count(file('filename'));<🎜> <🎜>echo $line;<🎜> <🎜>?>
The second method is very inefficient because it needs to save the contents of the file http://www.bkjia.com/PHPjc/1014281.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1014281.htmlTechArticlePHP provides two implementation methods for obtaining the number of file lines. Although the second is simple and easy to understand, the first The first one is the most efficient: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ?php $file_path...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template