$content?=?file("test.txt");
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、成都小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了隆德免費(fèi)建站歡迎大家使用!
$randContent?=?array_rand($content,5);
echo?implode("br?/",$randContent);
第一行使用file把把整個(gè)文件讀入一個(gè)數(shù)組中
第二行使用array_rand在數(shù)組中隨機(jī)取出5個(gè)元素
第三行將取出的5個(gè)數(shù)組中間添加br?/標(biāo)簽并打印出來
file
把整個(gè)文件讀入一個(gè)數(shù)組中
file?(?string?$filename?,?int?$flags?=?0?,?resource?$context?=???)?:?array
array_rand
從數(shù)組中隨機(jī)取出一個(gè)或多個(gè)隨機(jī)鍵
array_rand?(?array?$array?,?int?$num?=?1?)?:?int|string|array
implode
將一個(gè)一維數(shù)組的值轉(zhuǎn)化為字符串
implode?(?string?$glue?,?array?$pieces?)?:?string
第一步,讀取txt的文件。假設(shè)為a.txt
$content = file_get_content('a.txt'); //讀取文件內(nèi)容存入變量。
第二步,存入數(shù)據(jù)庫
mysql_query("insert 表名 (字段名) values('".$content."'));
Ps:文件是上傳的,上傳后的臨時(shí)文件名是:$_FILE['tmp_name']
/**
*?讀文件
**/
function?read_file($filename)
{
$fp?=?fopen($filename,?"r")?or?die("couldn't?open?$filename");
$read?=?fread($fp,?filesize($filename));
fclose($fp);
return?$read;
}
/**
*?寫文件
**/
function?write_file($filename,?$buffer)
{
$fp?=?fopen($filename,?"w")?or?die("couldn't?open?$filename");
flock(?$fp,?LOCK_EX?);
$write?=?fputs($fp,?$buffer);
flock(?$fp,?LOCK_UN?);
fclose($fp);
return?true;
}
/**
*?修改(只是追加內(nèi)容)
**/
function?append_to_file($filename,?$buffer)
{
$fp?=?fopen($filename,?"a")?or?die("couldn't?open?$filename");
flock(?$fp,?LOCK_EX?);
fputs($fp,?$buffer);
flock(?$fp,?LOCK_UN?);
fclose($fp);
return?true;
}
/**
*?測(cè)試
**/
$str?=?read_file('test.txt');
echo?$str;
write_file('test2.txt',?$str);
append_to_file('test2.txt',?"ABCD");
form action='' method="post"
輸入字母:
input type="text" name="cs"
input type="submit"
/form
?php
//獲取post值
$cs = empty($_POST['cs']) ? "" : $_POST['cs'];
//post值為空直接返回
if($cs==""){return '';}
//打開diqu.txt文件資源
$file = fopen("diqu.txt", "r") or exit("未找到文件!");
//逐行查找post傳遞的字符
while(!feof($file))
{
//如果找到post傳遞的字符就返回該行的值
if($val = strstr(fgets($file),$cs)){echo str_replace($cs,'',$val);return "";}
}
//關(guān)閉文件
fclose($file);
?
網(wǎng)頁題目:php讀出txt數(shù)據(jù)的簡(jiǎn)單介紹
網(wǎng)站鏈接:http://vcdvsql.cn/article28/ddijecp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、商城網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、營銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)