How can a separator be added at the end of each line in a text file created using PHP?
To add a separator at the end of each line in a text file created using PHP, you can simply append the separator to each line before writing it to the file. This can be done by concatenating the separator to the end of each line before writing it using the fwrite() function. By doing this for each line, you can ensure that a separator is added at the end of each line in the text file.
<?php
$lines = array("Line 1", "Line 2", "Line 3");
$separator = " | "; // Define the separator
$file = fopen("output.txt", "w");
foreach ($lines as $line) {
fwrite($file, $line . $separator . PHP_EOL); // Append separator to each line
}
fclose($file);
?>
Keywords
Related Questions
- What steps can be taken to troubleshoot and resolve issues with data not being transferred from one PHP script to another in a web application?
- What are the potential pitfalls of using multiple if statements in Flash to change backgrounds based on PHP variables?
- Can you provide an example of using modulo division in PHP to control image layout on a webpage?