導航:首頁 > 編程語言 > php文本行數

php文本行數

發布時間:2025-09-28 13:16:53

A. php寫入數據到php文件第2行 以及更改第3行數據 會的來

下面是完整的代碼:
//根據你的PHP設置自已可以把<?php替換為<?
<?php
//要更改的php文件
$filename = 'id.php';
//要插入的內容
$addContent = "wole\n";
//要更改的內容
$updateContent="qwe\n";

// 確定文件存在並且可寫。
if (is_writable($filename)) {
// 將文件讀入數組,每行是一條記錄
$lines = file ($filename);

// 使用寫入方式打開打開$filename,文件指針將會在文件的開頭
if (!$handle = fopen($filename, 'w')) {
print "不能打開文件 $filename";
exit;
}

//在數組中循環,當到達第2行時插入新的內容。
foreach ($lines as $line_num => $line) {
if($line_num==1){
//將$addContent寫入到文件中。
if (!fwrite($handle, $addContent)) {
print "不能寫入到文件 $filename";
exit;
}
}
//寫入原來的行內容到文件中
if (!fwrite($handle, $line)) {
print "不能寫入到文件 $filename";
exit;
}
}

//更改數據第3行zxcv為qwe,把上面foreach注釋掉,打開下面注釋掉的代碼
// foreach ($lines as $line_num => $line) {
// if($line_num==2){
// //將$updateContent寫入到文件中。
// if (!fwrite($handle, $updateContent)) {
// print "不能寫入到文件 $filename";
// exit;
// }
// }
// else{
// //寫入原來的行內容到文件中
// if (!fwrite($handle, $line)) {
// print "不能寫入到文件 $filename";
// exit;
// }
// }
// }

fclose($handle);

} else {
print "文件 $filename 不可寫";
}
?>

B. php將數組元素按行寫入文本文件

<?php
$arr=array('aa','bb','cc');
$str=implode(" ",$arr);
file_put_contents("A.txt",$str);
?>

閱讀全文

與php文本行數相關的資料

熱點內容
單片機發光二極體電路圖 瀏覽:453
php在線考試系統教程交流 瀏覽:888
盒馬鮮生app怎麼了 瀏覽:398
如何控制手機上網app 瀏覽:966
加密視頻防復制 瀏覽:893
文檔怎樣組成文件夾 瀏覽:233
pdf國外經典教材 瀏覽:530
數論基礎pdf 瀏覽:879
php架構設計 瀏覽:5
華為文件夾找字體 瀏覽:726
php中刷新頁面 瀏覽:508
oracle命令行查詢 瀏覽:808
ccf程序員 瀏覽:231
命令與征服裡面的工程師如何運用 瀏覽:350
excel怎麼復制加密 瀏覽:590
安卓手機拍照怎麼弄貼紙 瀏覽:516
php文本行數 瀏覽:849
php優秀開源項目 瀏覽:482
本科程序員有必要考非全碩士嗎 瀏覽:302
gta為什麼一直重復顯示連接伺服器 瀏覽:911