问题描述
我们有300多个txt文件,其中基本上是电子邮件的副本,每个txt文件都具有以下格式:
We have 300+ txt files, of which are basically replicates of an email, each txt file has the following format:
To: [email protected]
Subject: blabla
From: [email protected]
Message: Hello World!
目的是编写一个PHP脚本,该脚本会爬网每个文件(所有文件都位于同一目录中),并在发件人"字段中打印出每个唯一"电子邮件地址的列表.这个概念很简单.
The aim is to write a PHP script, which crawls through each file (all located within the same directory), and prints out a list of each 'unique' email address in the from field. The concept is very easy.
有人可以在这里向我指出正确的方向吗?到目前为止,我已经设法使我的PHP脚本读取目录中所有文件的内容并输出结果:
Can anyone point me in the right direction here? So far, I have managed to get my PHP script to read the contents of all of the files within the directory and output the result:
<?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);
?>
所以现在,我需要能够读取和打印每个文件的第3行.我认为它就像在foreach循环中添加数组以回显特定行一样简单?我确实尝试过,但是总的来说,有点混乱:
So now, I need to be able to read and print line 3 of each of the files. I assume that its as simple as adding in array within the foreach loop to echo a specific line? I did try this, but synatctally, it's a bit of a mess:
<?php
$directory = "emails/";
$dir = opendir($directory);
**$lines = file($filename);**
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>$lines[2]</td></tr>\n";**
}
echo '</table>';
}
}
closedir($dir);
?>
有人可以在这里将我推向正确的方向吗?
Can anyone push me in the right direction here?
推荐答案
使用命令行工具更容易做到这一点:
This is far easier to do with command line tools:
exec("grep '^From: ' *.txt|uniq", $output);
这将仅返回所有文本文件中以From:
开头的行.简单的子字符串提取即可为您提供实际的电子邮件地址,并且为您节省了大量PHP的工作.
That'll return ONLY the lines that start with From:
from all the text files. A simple substring extraction gets you the actual email address, and you've saved yourself a lot of PHP busywork.
这篇关于在PHP中读取多个文本文件的内容并打印出特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!