'In php, how do I load txt file as a string?

I have a text file that contains paragraphs of text. I want to load it as a string in php, and then collect all the e-mail addresses within it as an array. However, how do I initially load the text file contents as the string? Restated, I have

$string = **WITHIN TEXT FILE "[email protected] MIME-Version: bla bla bla [email protected]";
$matches = array();
$pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'
preg_match($pattern, $string, $matches);

How do I load the text from the text file into the string variable?

Thanks in advance!

php


Solution 1:[1]

$fileContent = file_get_contents($url);

Check the reference at http://php.net/manual/en/function.file-get-contents.php

Solution 2:[2]

The simplest way is file-get-contents!

$file_content = file_get_contents('/path/to/file');

Solution 3:[3]

$lines = file('file.txt');

foreach ($lines as $line) {
  // ...
}

Solution 4:[4]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bang Dao
Solution 2 Aif
Solution 3 Alec
Solution 4 troelskn