Skip to content
PHP Logo

How can one efficiently stop reading and extracting information from a text file after encountering a certain condition, such as a specific host name?

To efficiently stop reading and extracting information from a text file after encountering a specific condition, such as a specific host name, you can use a loop to read the file line by line and check for the condition. Once the condition is met, you can break out of the loop to stop reading the file.

php
<?php
$filename = 'example.txt';
$hostName = 'specificHostName';

$file = fopen($filename, 'r');

while (($line = fgets($file)) !== false) {
    // Extract information from the line
    // Check if the line contains the specific host name
    if (strpos($line, $hostName) !== false) {
        break; // Stop reading the file
    }
}

fclose($file);
?>