Skip to content
PHP Logo

How can I insert specific data into a certain line in a PHP file?

To insert specific data into a certain line in a PHP file, you can read the contents of the file, modify the specific line where you want to insert the data, and then write the updated contents back to the file. You can achieve this by using functions like file_get_contents(), explode(), implode(), and file_put_contents().

php
// Read the contents of the PHP file into an array
$lines = file('your_file.php');

// Insert specific data into a certain line
$specific_data = "Your specific data";
$line_number = 5; // Line number where you want to insert the data
array_splice($lines, $line_number - 1, 0, $specific_data);

// Write the updated contents back to the file
file_put_contents('your_file.php', implode('', $lines));