2012-03-18 60 views
1

我有一個郵件文件讀和解析的郵件文件

我想讀/解析郵件文件的頭(只頭)

我想讀這條線的頭部(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: <[email protected]> 
Message-ID: <[email protected]> 
Date: Thu, 15 Mar 2012 03:01:18 -0700 (PDT) 
From: Black Dream <[email protected]> 
Reply-To: Black Dream <[email protected]> 
Subject: Need help installing your certificate? 
To: "[email protected]" <[email protected]> 
In-Reply-To: <[email protected]> 
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)它似乎更快,但它讀取所有的文件,我無法讀取文件的一部分

回答

0

你或許最快的搜索結果使用從外殼的sed分割文件兩個部分基於內容類型的字符串,然後得到的結果從系統命令後面:

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

你可能會需要一個完整路徑mail.txt文件。試試看,並比較速度。

+0

謝謝,但你的命令已經讀取了所有的消息文件,不只是部分! – 2012-03-18 04:54:56

+0

file_get_contents:在0.0002秒內生成的頁面。 Sed命令:在0.0054秒內生成頁面。 你在開玩笑嗎? – 2012-03-18 05:12:34

+0

該命令應該讀取整個文件並返回前半部分作爲輸出。我不確定它會不會更快。顯然不是。 – davidethell 2012-03-18 05:31:29