PHP脚本打印一个特定的值旁边的字符串,从多个文本文件在同一目录


PHP script to print a specific value next to a string, from multiple text files in same directory?

我有50个txt文件,其中基本上是电子邮件的副本,每个txt文件有以下格式:

To: blabla@examplecom 
Subject: blabla 
From: bla1@example.com 
Message: This is a test message

目的是编写一个PHP脚本,该脚本遍历每个文件(都位于同一目录中),并在from字段中打印出每个"唯一"电子邮件地址的列表。这个概念很简单。

谁能给我指个正确的方向?到目前为止,我已经设法让我的PHP脚本读取目录中所有文件的内容并输出结果:
<?php
$directory = "emails/";
$dir = opendir($directory);
while (($file = readdir($dir)) !== false) {
  $filename = $directory . $file;
  $type = filetype($filename);
  if ($type == 'file') {
     $contents = file_get_contents($filename);
     $items = explode('/n', $contents);
     echo '<table width="500" border="1" cellpadding="4">';
     foreach ($items as $item) {
       echo "<tr><td>$item</td></tr>'n";
     }
     echo '</table>';
  }
}
closedir($dir);
?> 

所以现在,我需要能够从每个文件中检索'From: '字符串旁边分配的值,然后在列表中显示该值的输出。

这里有人能给我指个方向吗?任何代码解释都是有益的,因为我理解这个概念和我需要做的事情,但语法上我很挣扎!

$to = strstr ($item, ' to: ');
$from = strstr ($item, ' from: ');
$subject = stristr($item, ' subject: ');
$message = strstr ($item, ' message: ');

if($to !== FALSE) echo $to;
elseif($from !== FALSE) echo $from;elseif($subject !== FALSE) echo $subject;
elseif($message !== FALSE) echo $message;

这将获得您正在寻找的值。把它放到foreach循环中