複製代碼 代碼如下:
$arrInsert = insertContent("array.php", "abcdef", 3, 10);
unlink("array.php");
foreach($arrInsert as $value)
{
file_put_contents("array.php", $value, FILE_APPEND);
}
function insertContent($source, $s, $iLine, $index) {
$file_handle = fopen($source, "r");
$i = 0;
$arr = array();
while (!feof($file_handle)) {
$line = fgets($file_handle);
++$i;
if ($i == $iLine) {
if($index == strlen($line)-1)
$arr[] = substr($line, 0, strlen($line)-1) . $s . "n";
else
$arr[] = substr($line, 0, $index) . $s . substr($line, $index);
}else {
$arr[] = $line;
}
}
fclose($file_handle);
return $arr;
}
//在多資料我們儲存資料都是用資料庫教程來操作,上面我們就是把資料以X格式存在文本中了,現在我要像操作資料庫一樣的,想刪除那行就那行,儲存資料也一樣,怎麼讀取第幾行就第幾行了,所以我就寫出來了php 在檔案指定行插入資料執行個體哦。
?>
$iLine:為第幾行,$index為第幾個字元之前
http://www.bkjia.com/PHPjc/321832.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321832.htmlTechArticle複製代碼 代碼如下: $arrInsert = insertContent("array.php", "abcdef", 3, 10); unlink("array.php"); foreach($arrInsert as $value) { file_put_contents("array.php", $value, FIL...