资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php指定读取文件行数据 PHP读取文件

php读取txt文件指定行的内容并显示

?php

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序制作、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了东丰免费建站欢迎大家使用!

function getFileRows($filename,$start,$num=0)

{

$rowsdata = array();

$lines = file( $filename );

$start = $start -1;

$num = $num == 0 ? count($lines)-$start : $num;

for($i=0;$i$num; $i++)

{

$k = $start + $i;

$rowsdata[] = $lines[$k];

}

return $rowsdata;

}

print_r(getFileRows('1.txt',3,1)); //第三行

print_r(getFileRows('1.txt',5,1)); //第5行

print_r(getFileRows('1.txt',7,1)); //第7行

?

php如何读取文本指定的内容?

php读取文件内容:

-----第一种方法-----fread()--------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$fp = fopen($file_path,"r");

$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来

echo $str = str_replace("\r\n","br /",$str);

}

?

--------第二种方法------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中

$str = str_replace("\r\n","br /",$str);

echo $str;

}

?

-----第三种方法------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$fp = fopen($file_path,"r");

$str = "";

$buffer = 1024;//每次读取 1024 字节

while(!feof($fp)){//循环读取,直至读取完整个文件

$str .= fread($fp,$buffer);

}

$str = str_replace("\r\n","br /",$str);

echo $str;

}

?

-------第四种方法--------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$file_arr = file($file_path);

for($i=0;$icount($file_arr);$i++){//逐行读取文件内容

echo $file_arr[$i]."br /";

}

/*

foreach($file_arr as $value){

echo $value."br /";

}*/

}

?

----第五种方法--------------------

?php

$file_path = "test.txt";

if(file_exists($file_path)){

$fp = fopen($file_path,"r");

$str ="";

while(!feof($fp)){

$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。

}

$str = str_replace("\r\n","br /",$str);

echo $str;

}

?

PHP里如何读取文件的指定一行?

?php

$c = getLine('./a.txt', 10); // 读取a.txt文件第10行内容

echo $c;

/**

* 获取指定行内容

*

* @param $file 文件路径

* @param $line 行数

* @param $length 指定行返回内容长度

*/

function getLine($file, $line, $length = 4096){

$returnTxt = null; // 初始化返回

$i = 1; // 行数

$handle = @fopen($file, "r");

if ($handle) {

while (!feof($handle)) {

$buffer = fgets($handle, $length);

if($line == $i) $returnTxt = $buffer;

$i++;

}

fclose($handle);

}

return $returnTxt;

}

php如何获取txt文本指定行的指定数据?

如果直接使用file_get_contents来读取文件,那么在文件很大的时候会很占内容,比如这个文件有1GB的时候。

这个时候使用传统的文件操作方式就好的多,因为是查找嘛,逐行读取匹配应该也是可以的,下面是我的一个建议,不知道是否满足你的要求,可以看下:

//

需要查找的内容

$search

=

'bcd';

//

打开文件

$res

=

fopen('a.txt',

'r');

while

($line

=

fgets($res,

1024))

{

//

根据规则查找

if

(strpos($line,

$search)

===

0)

{

//

根据既定规则取得需要的数据

echo

substr($line,

4,

-1);

//

这里就是你想得到的

break;

}

}

//

关闭文件

fclose($res);

PHP怎么获取文件的行数

有二种方法可以实现,分别如下:

第一种:

?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;  

?

第二 种:

?php  

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

echo $line;  

?

php里有没有指定读取第几行的函数

php提供了内置函数fgets(),从文件指针中读取一行。代码如下:?php $file=fopen("D:\\CHENCHENG\\myqq.txt","r"); while(! feof($file)){ $rows = fgets($file); echo "你要的第三行内容就是$rows[2]";}


新闻标题:php指定读取文件行数据 PHP读取文件
转载注明:http://cdkjz.cn/article/dochheh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220