What are some best practices for handling varying numbers of links in text files when reading them with PHP?

When reading text files with varying numbers of links in PHP, it is important to dynamically handle the extraction of these links. One way to achieve this is by using regular expressions to identify and extract the links from the text file. By using preg_match_all() function, we can capture all links in the text file and store them in an array for further processing.

// Read the text file
$file = file_get_contents('example.txt');

// Use regular expression to extract all links
preg_match_all('/\bhttps?:\/\/\S+\b/', $file, $matches);

// Loop through the extracted links
foreach ($matches[0] as $link) {
    echo $link . "\n";
}