|
$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;
}
//在多數(shù)據(jù)我們存儲(chǔ)數(shù)據(jù)都是用數(shù)據(jù)庫教程來操作,上面我們就是把數(shù)據(jù)以X格式存在文本中了,現(xiàn)在我要像操作數(shù)據(jù)庫一樣的,想刪除那行就那行,保存數(shù)據(jù)也一樣,怎么讀取第幾行就第幾行了,所以我就寫出來了php 在文件指定行插入數(shù)據(jù)實(shí)例哦。
?>
$iLine:為第幾行,$index為第幾個(gè)字符之前
php技術(shù):php 在文件指定行插入數(shù)據(jù)的代碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。