PHP讀取TXT文本內容的五種實用方法小結
在Web開發(fā)中,我們經(jīng)常需要讀取和處理文本文件。PHP作為一種流行的服務器端腳本語言,提供了多種方法來讀取TXT文本內容。本文將介紹五種不同的PHP教程,幫助您學習如何使用PHP讀取TXT文本內容。PHP讀取文件內容在實際開發(fā)當中,還是比較常見的,所以今天我就給大家分享幾種讀取的方法,大家可以選擇一種最適合的就行了。
第一種,使用fread函數(shù):
<?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);
fclose($fp);
}
?>第二種,用file_get_contents函數(shù):
<?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;
}
?>第三種,用fopen函數(shù):
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次讀取 1024 字節(jié)
while(!feof($fp)){//循環(huán)讀取,直至讀取完整個文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
fclose($fp);
}
?>第四種方法,使用file函數(shù):
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行讀取文件內容
echo $file_arr[$i]."<br />";
fclose($file_arr);
}
}
?>第五種,還是使用fopen函數(shù):
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行讀取。如果fgets不寫length參數(shù),默認是讀取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
fclose($fp);
}
?>
當然,開啟資源后,記得使用fclose($fp);關閉一下,不然的話,會消耗服務器的資源。
方法補充
除了上文的方法,小編還為大家整理了其他一些PHP讀取TXT文本的方法,希望對大家有所幫助
php讀取文件內容的三種方法:
//**************第一種讀取方式*****************************
代碼如下:
header("content-type:text/html;charset=utf-8"); //告訴php預處理器將內容已utf8的格式傳遞給瀏覽器
//文件路徑
$file_path="text.txt";
//判斷是否有這個文件
if(file_exists($file_path)){
if(fp=fopen(file_path,"a+")){
//讀取文件
conn=fread(fp,filesize($file_path));
//替換字符串
conn=strreplace("rn","<br/>",conn);
echo $conn."<br/>";
}else{
echo "文件打不開";
}
}else{
echo "沒有這個文件";
}
fclose($fp);
//*******************第二種讀取方式***************************
代碼如下:
header("content-type:text/html;charset=utf-8");
//文件路徑
$file_path="text.txt";
conn=filegetcontents(file_path);
conn=strreplace("rn","<br/>",filegetcontents(file_path));
echo $conn;
fclose($fp);
//******************第三種讀取方式,循環(huán)讀取*****************
代碼如下:
header("content-type:text/html;charset=utf-8");
//文件路徑
$file_path="text.txt";
//判斷文件是否存在
if(file_exists($file_path)){
//判斷文件是否能打開
if(fp=fopen(file_path,"a+")){
$buffer=1024;
//邊讀邊判斷是否到了文件末尾
$str="";
while(!feof($fp)){
str.=fread(fp,$buffer);
}
}else{
echo "文件不能打開";
}
}else{
echo "沒有這個文件";
}
//替換字符
str=strreplace("rn","<br>",str);
echo $str;
fclose($fp); 利用fopen,file,file_get_contents函數(shù)來實現(xiàn)讀取文本文件內容
//fopen 讀取文件實例,代碼如下:
$path ='a.txt';
$fp=fopen($file,"r");//以只讀的方式打開文件
while(!(feof($fp)))
{
$text=fgets($fp);//讀取文件的一行
echo $text;
}
//file_get_contents讀取文件,代碼如下:
if( file_exists( $path ) )
{
$body = file_get_contents($path);
echo $body ;//輸入文件內容
}
else
{
echo "文件不存在 $path";
}//開源代碼phpfensi.com
//讀取文本文件,代碼如下:
$cbody = file($path);
print_r($cbody); //因為file讀取出來的文件是以數(shù)組形式保存的,所以用print_r輸出。到此這篇關于PHP讀取TXT文本內容的五種實用方法小結的文章就介紹到這了,更多相關PHP讀取TXT內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Thinkphp和onethink實現(xiàn)微信支付插件
這篇文章主要為大家詳細介紹了Thinkphp和onethink實現(xiàn)微信支付插件,感興趣的小伙伴們可以參考一下2016-04-04
The specified CGI application misbehaved by not returning a
The specified CGI application misbehaved by not returning a complete set of HTTP headers2011-03-03
php 智能404跳轉代碼,適合換域名沒改變目錄的網(wǎng)站
適合于換域名,但是目錄沒有改變的網(wǎng)站.也可以用做301定向.轉自于落伍,收集過來.怕以后需要.需要的兄弟直接copy過去即可.2010-06-06

