【发布时间】:2011-11-02 23:02:42
【问题描述】:
我们有 300 多个 txt 文件,其中基本上是电子邮件的复制品,每个 txt 文件的格式如下:
To: blabla@hotmail.com
Subject: blabla
From: bla1@hotmail.com
Message: Hello World!
目的是编写一个 PHP 脚本,它会爬取每个文件(所有文件都位于同一目录中),并在发件人字段中打印出每个“唯一”电子邮件地址的列表。这个概念很简单。
任何人都可以在这里指出正确的方向吗?到目前为止,我已经设法让我的 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);
?>
所以现在,我需要能够读取和打印每个文件的第 3 行。我假设它就像在 foreach 循环中添加数组以回显特定行一样简单?我确实尝试过这个,但从语法上讲,它有点乱:
<?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);
?>
谁能在这里把我推向正确的方向?
【问题讨论】:
-
其他人可以帮忙吗??