Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Comments for: galloway20021105.php3

Message # 1014101:
Date: 11/07/02 06:45
By: Danny Shepherd Profile
Subject: Message Parsing

Here's how I do my message parsing - no regexps so it could be a bit faster than the implementation shown here.

<?php

// Grab the entire message first.
$stdin=fopen("php://stdin","r");

$message=$headerList="";
$headers=Array();

while (!feof($stdin))
$message.=fread($stdin,1024);
fclose($stdin);

// Make sure EOLs are \r\n
$message=str_replace("\n","\r\n",$message);

// Remove the single blank line at the top of the message
// Don't know where this comes from (added by PHP at the stdin phase?)
list(,$message)=explode("\r\n",$message,2);

// Split the message into headers and body
list($headerList,$message)=explode("\r\n\r\n",$message,2);

// Make an array of headers (special handling for multiline headers)
$headerList=explode("\r\n",str_replace("\r\n\t"," ",$headerList));

// Make an assoc array of headers
// Note the extra param in explode()!
foreach($headerList as $header)
{
list($key,$header)=explode(':',$header,2);
$headers[strtolower($key)]=substr($header,1);
}

var_dump($headers);
var_dump($message);

?><?php

// Grab the entire message first.
$stdin=fopen("php://stdin","r");

$message=$headerList="";
$headers=Array();

while (!feof($stdin))
$message.=fread($stdin,1024);
fclose($stdin);

// Make sure EOLs are \r\n
$message=str_replace("\n","\r\n",$message);

// Remove the single blank line at the top of the message
// Don't know where this comes from (added by PHP at the stdin phase?)
list(,$message)=explode("\r\n",$message,2);

// Split the message into headers and body
list($headerList,$message)=explode("\r\n\r\n",$message,2);

// Make an array of headers (special handling for multiline headers)
$headerList=explode("\r\n",str_replace("\r\n\t"," ",$headerList));

// Make an assoc array of headers
// Note the extra param in explode()!
foreach($headerList as $header)
{
list($key,$header)=explode(':',$header,2);
$headers[strtolower($key)]=substr($header,1);
}

var_dump($headers);
var_dump($message);

?>

Previous Message | Next Message


Comments:
How to take online feedbak from user?Anwarul06/10/05 07:24
RE: Message ParsingBilal Farooq09/24/03 14:14
RE: Mail Parsing - PEARmontri01/25/03 08:57
Mail Parsing - PEARNil Angsioco12/09/02 19:52
Mime ParserStephen VanDyke11/21/02 16:10
Mime ParserStephen VanDyke11/21/02 16:10
PHP ProgrammingMahadi11/12/02 09:43
RE: Message ParsingMichael Galloway11/11/02 11:30
Message ParsingDanny Shepherd11/07/02 06:45
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.