本文目錄一覽:
- 1、B.php中讀取文件內容的幾種方法
- 2、php如何將文本域的內容拆分為數組,逐行寫入數據庫
- 3、php中如何分割文本
- 4、用PHP讀取一個文本文件,以空格為分界符,把每個分段存儲在數組中。怎麼寫?
- 5、php 逐行讀取txt 並,分隔判斷
B.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);
}?123456789101112131415161718
——–第二種方法————
?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;
}?123456789101112131415161718192021222324
—–第三種方法————?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;
}?123456789101112131415161718192021222324252627282930313233343536373839404142
——-第四種方法————–?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 /”;
}*/}?1234567891011121314151617181920212223242526272829303132333435
—-第五種方法——————–
?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;
}?123456789101112131415161718192021222324252627282930313233343536
php如何將文本域的內容拆分為數組,逐行寫入數據庫
PHP 中的fgets() 函數可以實現
fgets() 函數從文件指針中讀取一行。
fgets(file,length)
參數說明
file 必需。規定要讀取的文件。
length 可選。規定要讀取的字節數。默認是 1024 字節。
詳細說明
從 file 指向的文件中讀取一行並返回長度最多為 length – 1 字節的字符串。碰到換行符(包括在返回值中)、EOF 或者已經讀取了 length – 1 字節後停止(要看先碰到那一種情況)。如果沒有指定 length,則默認為 1K,或者說 1024 字節。
若失敗,則返回 false。
注釋:length 參數從 PHP 4.2.0 起成為可選項,如果忽略,則行的長度被假定為 1024 字節。從 PHP 4.3 開始,忽略掉 length 將繼續從流中讀取數據直到行結束。如果文件中的大多數行都大於 8 KB,則在腳本中指定最大行的長度在利用資源上更為有效。
從 PHP 4.3 開始本函數可以安全用於二進制文件。早期的版本則不行。
如果碰到 PHP 在讀取文件時不能識別 Macintosh 文件的行結束符,可以激活 auto_detect_line_endings 運行時配置選項。
例如:
test.txt 文本內容如下:
Hello, this is a test file.
There are three lines here.
This is the last line.
?php
//讀取一行
$file = fopen(“test.txt”,”r”);
echo fgets($file);
fclose($file);
?
輸出:
Hello, this is a test file.
?php
//循環讀取每一行
$file = fopen(“test.txt”,”r”);
while(! feof($file)) {
echo $str = fgets($file). “br /”;
//這裡可以逐行的寫入數據庫中
//mysql_query(“insert into table(id,contents) values(NULL,'”.$str.”‘)”);
}
fclose($file);
?
輸出:
Hello, this is a test file.
There are three lines here.
This is the last line.
php中如何分割文本
PHP用空格分割文本為數組的方法:
php逐行讀取文本文件,然後處理空格分隔文本,輸出為數組的方法。
文本文檔text.txt內容:
1 字段1 字段2 2 字段1 字段2 3 字段1 字段2 4 字段1 字段2
文本和文本之間用空格隔開,用php經過處理,輸出為數組,以下是代碼:
php $file = fopen(“text.txt”, “r”) or exit(“Unable to open file!”);
while(!feof($file)) { $arr = split(‘ ‘ , fgets($file)); print_r($arr); } fclose($file);
輸出結果:
Array ( [0] = 1 [1] = 字段1 [2] = 字段2 ) Array ( [0] = 2 [1] = 字段1 [2] = 字段2 ) Array ( [0] = 3 [1] = 字段1 [2] = 字段2 ) Array ( [0] = 4 [1] = 字段1 [2] = 字段2 )
這樣就實現了PHP用空格分割文本為數組的方法.
用PHP讀取一個文本文件,以空格為分界符,把每個分段存儲在數組中。怎麼寫?
$str = “你好1 你好2 你好3”;
//$str = file_get_contents(“/path/to/file/xxx.xxx”); //讀取文件內容用這個
$arr = explode(” “, $str);
$index = rand(0, count($arr) – 1); //產生隨機數
echo $arr[$index];
exit;
原創,望採納
php 逐行讀取txt 並,分隔判斷
?
$file = file_get_contents(‘text.txt’);
//讀取文件
$lines = explode(‘\n’, $file);
//按行分割字符串
echo ‘table’;
//用表格輸出
for($lines as $line){
echo ‘tr’;
//分行
$keys = explode(‘,’, $line);
//按逗號分割
for($keys as $key){
echo “td$key/td”;
//輸出每行中的各列
}
echo ‘/tr’;
}
echo ‘/table’;
原創文章,作者:HAKJ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/133209.html