⑴ php 怎麼修改txt文本
PHP有兩種方法讀寫文件,方法一、file、file_get_contents、file_put_contents三個函數整體讀寫文本,適合文本文件不太大的情況。兩個函數的的典型應用是:
$text=file_get_contents('a.txt');//把文本文件的所有內容取到字元串變數$text裡面
$arr=file('a.txt');//把文本文件的所有內容,取到數組$arr裡面,$arr[0]就是第一行,以此類推
$arr[1]='abc';//或者通過其它途徑修改變數值
$text=implode($arr);//把數組連接為字元串
file_put_contents('a.txt', $text);//把字元串變數的內容寫入到文本文件裡面。
方法二、使用fopen、fgets、fputs、fclose函數讀寫文件,可以應付特大文件的修改。文件的修改一般方法是新建立一個文件,把源文件全部掃描一遍,遇到需要的部分進行處理,最後刪除源文件,更名新文件。例如下面的代碼把a.txt裡面的abc修改為def:
$fp1=fopen('a.txt','r');
$fp2=fopen('a.tmp','w');
while(!feof($fp1)){
$line=fgets($fp1);
$line=str_replace('abc','def',$line);
fputs($fp2,$line);
}
flcose($fp1);
fclose($fp2);
unlike('a.txt');
rename('a.tmp','a.txt');
⑵ 用PHP,怎麼修改txt文本內的內容
<?php
header("Content-type:text/html;charset=gbk");
if(isset($_POST['submit'])){
$editContent=$_POST['editContent'];//獲取輸入框的內容
$res=file_put_contents("test.txt",$editContent);//執行修改
if($res){
echo'內容修改成功!';
}else{
echo'內容修改失敗!';
}
}else{
echo'請做出修改....';
}
?>
<formmethod="post"action="">
<textareaname="editContent"cols="100"rows="15">
<?phpechofile_get_contents("test.txt")?>
</textarea>
<buttonname="submit">確認</button>
</form>
⑶ 關於PHP引用txt代碼..
可以看看這個
http://www.phptogether.com/?s=txt+%E5%88%86%E9%A1%B5
⑷ php如何實現對txt的讀取
//打開文件
$fp = fopen("4.txt");
//循環讀取每一行
while(!feof($fp)){
//讀取當前行的內容
$line = fget($fp);
if($line !=""){
//跳轉
header("location:"+$line);
}
}
fclose($fp);
大概這樣吧,很久沒寫php,不知道有沒寫錯
⑸ PHP怎麼寫入TXT文檔
php 寫入txt:
PHP
function writelog($str)
{
$open=fopen("log.txt","a" );
fwrite($open,$str);
fclose($open);
}
'a' 寫入方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創建之。
'a+' 讀寫方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創建之。
php txt 換行
"\r\n"
不可用單引號.
⑹ 請教php顯示txt文件內容
<?php
$handle = fopen ("/tmp/inputfile.txt", "r");
while (!feof ($handle)) {
$buffer = fgets($fd, 4096);
echo $buffer;
}
fclose ($handle);
?>
⑺ php 如何創建txt文件
看手冊,文件操作部分,比如 file_put_contents 就能滿足要求
<?php
$file='people.txt';
//Thenewpersontoaddtothefile
$person="JohnSmith ";
//Writethecontentstothefile,
//usingtheFILE_
//andtheLOCK_
file_put_contents($file,$person,FILE_APPEND|LOCK_EX);
?>
⑻ PHP刪除TXT文本內容
那就很好改了。
<?php
$filename="aaa.txt";//定義操作文件
$dellinekey='13800';//要刪除的行關鍵字
$delcount=0;//已刪除的行數
$farray=file($filename);//讀取文件數據到數組中
for($i=0;$i<count($farray);$i++)
{
if($delcount==0&&substr_count($farray[$i],$dellinekey)>0)//先判斷是否已刪除一次,再判斷當前行是否包含關鍵字,是則刪除
{
$delcount++;//標記刪除一次
continue;
}
if(trim($farray[$i])<>"")//刪除文件中的所有空行
{
$newfp.=$farray[$i];//重新整理後的數據
}
}
$fp=@fopen($filename,"w");//以寫的方式打開文件
@fputs($fp,$newfp);
@fclose($fp);
?>
⑼ 求寫一段保存txt的php代碼
file_put_contents("code.txt",$_GET['msg']);//$_GET['msg']是獲取的msg
如果內容是追加方式,改成:
file_put_contents("code.txt",$_GET['msg'],FILE_APPEND);
⑽ PHP+TXT讀取文本內容並輸出
//在命令行cmd中運行,能看到3秒的效果
$content=file_get_contents('a.txt');
$arr=explode(" ",$content);
$count=ceil(count($arr)/20);
for($i=0;$i<$count;$i++){
$k=$i*20+20;
for($j=$i*20;$j<$k;$j++){
if(!empty($arr[$j])){
echo$arr[$j].PHP_EOL;
}
}
sleep(3);
echoPHP_EOL;
}
//有看不懂的步驟,可以追問