What best practices should be followed when parsing and manipulating data from a text file using PHP?
When parsing and manipulating data from a text file using PHP, it is important to follow best practices to ensure data integrity and security. This includes properly sanitizing input data, validating user input, handling errors gracefully, and using appropriate file handling functions.
<?php
// Open the text file for reading
$file = fopen("data.txt", "r");
// Read the contents of the file line by line
while (!feof($file)) {
$line = fgets($file);
// Manipulate the data as needed
// For example, you can split the line by a delimiter
$data = explode(",", $line);
// Process and output the data
echo "Name: " . $data[0] . ", Age: " . $data[1] . "<br>";
}
// Close the file
fclose($file);
?>