PHP code to insert data on a specified line in a file

  • 2020-03-31 20:41:16
  • OfStack

 
$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; 
} 
//We store data in multiple data in database tutorial to operation, we just above the data exist in X format in the text, and now I am going to like working with databases, want to delete the line is the line, the same to save the data, how to read lines which is which, so I just write the PHP line insert data instances specified in the file.
?> 

$iLine: is the line and $index before the character

Related articles: