How can PHP functions be effectively utilized to read and process data from a text file for input filtering purposes?
To read and process data from a text file for input filtering purposes in PHP, you can create a function that reads the file line by line, filters the input using PHP's built-in functions like filter_var(), and then processes the filtered data as needed.
<?php
// Function to read and process data from a text file
function processTextFile($filename) {
$file = fopen($filename, "r");
if ($file) {
while (($line = fgets($file)) !== false) {
// Filter the input data
$filteredData = filter_var($line, FILTER_SANITIZE_STRING);
// Process the filtered data
echo $filteredData . "<br>";
}
fclose($file);
} else {
echo "Error opening file.";
}
}
// Usage
$filename = "data.txt";
processTextFile($filename);
?>