What are some best practices for searching for text in a file using PHP?
Searching for text in a file using PHP involves reading the contents of the file line by line and checking for the presence of the desired text. One way to do this is by using the `file()` function to read the file into an array of lines, and then iterating through each line to search for the text using `strpos()` or `preg_match()`.
// Open the file for reading
$file = fopen('example.txt', 'r');
// Loop through each line in the file
while (!feof($file)) {
$line = fgets($file);
// Search for the text 'search_term' in the line
if (strpos($line, 'search_term') !== false) {
echo 'Text found in file: ' . $line;
}
}
// Close the file
fclose($file);