Home  >  Article  >  Backend Development  >  PHP code to insert data in specified line of file_PHP tutorial

PHP code to insert data in specified line of file_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:041082browse

Copy code The code is as follows:

$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;
}
//In Multidata, we use the database tutorial to store data. Above we just use X The format is stored in the text. Now I have to operate it like a database. If I want to delete the row, just use that row. The same goes for saving the data. How to read the row? So I wrote it out in php and specified it in the file. Row insert data instance.
?>

$iLine: which line is it, $index is which character before it

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321832.htmlTechArticleCopy the code as follows: $arrInsert = insertContent("array.php", "abcdef", 3, 10) ; unlink("array.php"); foreach($arrInsert as $value) { file_put_contents("array.php", $value, FIL...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn