php中读取txt文件的问题

PHP 2537 0 2013-07-01

php中读取txt文件的问题
<?PHP 
 $file_name="a.txt";
 $fp=fopen($file_name,'r');
  while(!feof($fp))
 {
  
  $buffer=fgets($fp,4096);
  if ($buffer=="abc")
      {echo "找到了";}
     
  echo $buffer."<br>";
 }
 fclose($fp);
?>

问题在于:我在a.txt中加入了一行abc,但是程序却不能执行到echo "找到了";
请问这是什么原因呢?

上一篇:php 去掉完全相同的重复数组

下一篇:PHP去掉HTML代码

讨论数量:3

天涯网魂 3 杠 5 星2013-07-01 11:10:52

PHP 读取文件内容代码(txt,js等)
<?php
/*
作者:bjf;
应用:读取文件内容;
*/
function read_file_content($FileName)
{
//open file
$fp=fopen($FileName,"r");
$data="";
while(!feof($fp))
{
//read the file
$data.=fread($fp,4096);
}
//close the file
fclose($fp);
//delete the file
//unlink($FileName);
//return the content from the file
echo $data;
}
read_file_content("a.html")
?>
fread与fgets的区别
fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止.
fgets :整行读取,遇到回车换行或结尾停止.在文本方式时使用. 

天涯网魂 3 杠 5 星2013-07-01 11:10:10

PHP 处理TXT文件(打开/关闭/检查/读取)
<?php
$filename=dirname(__FILE__)."/readfrom.txt";
$ofilename=dirname(__FILE__)."/writeto.txt";

if(!file_exists($filename)){
echo $filename." not found!";
exit;
}

$fp=fopen($filename,"r");
$fo = fopen($ofilename,"w");

while(!feof($fp)){

$record=fgets($fp);

if($record!=NULL){
$record_arr=explode("\t", $record);

fwrite($fo,$record,strlen($col));
}


}
fclose($fo);
fclose($fp);

?> 

天涯网魂 3 杠 5 星2013-07-01 11:09:30

<?PHP
$file_name="a.txt";
$fp=fopen($file_name,'r');
while(!feof($fp))
{

$buffer=fgets($fp);
if (preg_match("/^abc\s$/",$buffer))
{echo "找到了";}

echo $buffer."<br>";
}
fclose($fp);
?> 
改成这样,因为文本中有个换行符,用正则匹配一下就好了

请先登录再发表讨论。 2024-04-30

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链