php讀取文件內(nèi)容:
創(chuàng)新互聯(lián)公司是專業(yè)的登封網(wǎng)站建設公司,登封接單;提供成都做網(wǎng)站、網(wǎng)站設計,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行登封網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
-----第一種方法-----fread()--------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定讀取大小,這里把整個文件內(nèi)容讀取出來
echo $str = str_replace("\r\n","br /",$str);
}
?
--------第二種方法------------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//將整個文件內(nèi)容讀入到一個字符串中
$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 字節(jié)
while(!feof($fp)){//循環(huán)讀取,直至讀取完整個文件
$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++){//逐行讀取文件內(nè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參數(shù),默認是讀取1k。
}
$str = str_replace("\r\n","br /",$str);
echo $str;
}
?
直接把你要的文本復制出來
或者代碼比較重復的話可以使用批量替換
沒有什么比較好的解決方法
如果直接使用file_get_contents來讀取文件,那么在文件很大的時候會很占內(nèi)容,比如這個文件有1GB的時候。
這個時候使用傳統(tǒng)的文件操作方式就好的多,因為是查找嘛,逐行讀取匹配應該也是可以的,下面是我的一個建議,不知道是否滿足你的要求,可以看下:
//
需要查找的內(nèi)容
$search
=
'bcd';
//
打開文件
$res
=
fopen('a.txt',
'r');
while
($line
=
fgets($res,
1024))
{
//
根據(jù)規(guī)則查找
if
(strpos($line,
$search)
===
0)
{
//
根據(jù)既定規(guī)則取得需要的數(shù)據(jù)
echo
substr($line,
4,
-1);
//
這里就是你想得到的
break;
}
}
//
關閉文件
fclose($res);
網(wǎng)站名稱:php提取文本數(shù)據(jù)處理 php取指定文本
網(wǎng)站地址:http://muchs.cn/article42/doscgec.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、網(wǎng)站設計、、軟件開發(fā)、網(wǎng)站內(nèi)鏈、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)