Home > Backend Development > PHP Tutorial > PHP如何获取文件行数

PHP如何获取文件行数

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:44:36
Original
1082 people have browsed it

貌似没查到有直接获取文件行数的函数。

只能逐行计数么?

大文件直接 sys("wc -l 文件名") 是不是效率能高点

回复内容:

貌似没查到有直接获取文件行数的函数。

只能逐行计数么?

大文件直接 sys("wc -l 文件名") 是不是效率能高点

小文件直接 $line = count(file('filename'));
大文件可以考虑用 SplFileObject 处理。

逐行计数在行数比较多的时候会慢,比如用fgets默认读一行,每行都会产生函数呼叫开销。

可以用substr_count($str, "\n")数回车符的个数。

大文件的话,可以每次读1mb的片段,然后用substr_count数出行数,再把结果累加。

Related labels:
php
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