在php中,如何将文本文件作为字符串加载?

时间:2010-10-10 15:56:26

标签: php

我有一个包含文字段落的文本文件。我想在php中将其作为字符串加载,然后将其中的所有电子邮件地址作为数组收集。但是,我如何最初加载文本文件内容作为字符串?重申,我有

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

如何将文本文件中的文本加载到字符串变量中?

提前致谢!

4 个答案:

答案 0 :(得分:14)

$fileContent = file_get_contents($url);

检查http://php.net/manual/en/function.file-get-contents.php

上的参考

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

答案 3 :(得分:0)

最简单的方法是file-get-contents

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