【问题标题】:read and parsing the head of the mail file读取并解析邮件文件的头部
【发布时间】:2012-03-18 03:16:38
【问题描述】:

我有一个邮件文件

我想读取/解析邮件文件的头部(仅头部)

我想读到这一行(Content-Type:)这是标题的结尾

这是文件内容的示例

Received: from [41.43.66.49] by web121206.mail.ne1.yahoo.com via HTTP; Thu, 15 Mar 2012 03:01:18 PDT
X-Mailer: YahooMailWebService/0.8.117.340979
References: <20120315074428.615964901A4@sat51-alie.secureinternetnews.net>
Message-ID: <1331805678.39182.YahooMailNeo@web121206.mail.ne1.yahoo.com>
Date: Thu, 15 Mar 2012 03:01:18 -0700 (PDT)
From: Black Dream <more_ache@ymail.com>
Reply-To: Black Dream <more_ache@ymail.com>
Subject: Need help installing your certificate?
To: "admin@emiddy-hosting.com" <admin@emiddy-hosting.com>
In-Reply-To: <20120315074428.615964901A4@sat51-alie.secureinternetnews.net>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="1735753853-309369511-1331805678=:39182"

--1735753853-309369511-1331805678=:39182
Content-Type: multipart/alternative; boundary="1735753853-307665870-1331805678=:39182"

--1735753853-307665870-1331805678=:39182
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

=0A=0A=0A----- Forwarded Message -----=0AComodo Group, Inc. <e-mail= .....
.... hello hello hello ... the message body here

这是我正在处理的代码

$the_mail_data = array ();
$handle = fopen('mail.txt', 'r');
while (($buffer = fgets($handle)) !== false) {
        $break = explode ( ':' , $buffer );
        $the_mail_data[$break[0]] = $break[1];
        if ( $break[0] == 'Content-Type' )
        break;
}
fclose($handle);
    //Now i've the data in this array $the_mail_data();
    //print_r ( $the_mail_data );
    //echo $the_mail_data['subject'];

代码运行良好,但我需要更快的东西!

有没有更快的函数或更快的代码?

我现在正在尝试(file_get_contents),它似乎更快,但它读取了所有文件,我无法读取文件的一部分

【问题讨论】:

    标签: php


    【解决方案1】:

    您可能会获得最快的结果,使用 shell 中的 sed 根据 Content-Type 字符串将文件分成两部分,然后从系统命令中获取结果:

    $head = system("sed '1,/^Content-Type$/!d' < mail.txt");
    

    您可能需要 mail.txt 文件的完整路径。尝试一下并比较速度。

    【讨论】:

    • 谢谢,但是你的命令已经读取了所有的消息文件,而不只是一部分!!
    • file_get_contents:页面生成时间为 0.0002 秒。 Sed 命令:页面在 0.0054 秒内生成。你在开玩笑吗??
    • 该命令应该读取整个文件并将前半部分作为输出返回。我不确定它是否会更快。显然不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    相关资源
    最近更新 更多