Home > Backend Development > PHP Tutorial > PHP获取文件行数的方法_php技巧

PHP获取文件行数的方法_php技巧

WBOY
Release: 2016-05-16 20:14:10
Original
936 people have browsed it

本文实例讲述了PHP获取文件行数的方法。分享给大家供大家参考。具体分析如下:

提供两种实现方法,虽然第二种简单易懂,但是第一种效率最好

第一种:

<&#63;php 
$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; 
&#63;> 

Copy after login

第二种:

<&#63;php 
  $line = count(file('filename')); 
  echo $line; 
&#63;>

Copy after login

第二种方式因为要保存文件的内容,效率上会很差

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

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