Re: Reg exp to remove line feeds before lines starting with white space
| From: | lallous | Date: | Thu, 25 Jul 2002 13:02:48 +0000 |
| Subject: | Re: Reg exp to remove line feeds before lines starting with white space | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-109479@lists.php.net to get a copy of this message | ||
<?
$mem='Newsgroups: php.general
Path: news.php.net
Xref: news.php.net php.general:109465
Return-Path: <nille@comology.se>
Mailing-List: contact php-general-help@lists.php.net; run by ezmlm
Delivered-To: mailing list php-general@lists.php.net
Received1: (qmail 60843 invoked from network); 25 Jul 2002 10:36:29 -0000
Received: from unknown (HELO parker.comology.com) (213.187.218.7)
by pb1.pair.com with SMTP; 25 Jul 2002 10:36:29 -0000
Received2: (qmail 14308 invoked by uid 500); 25 Jul 2002 10:36:27 -0000
Date: Thu, 25 Jul 2002 12:36:27 +0200
To: php-general@lists.php.net
Message-ID: <20020725103627.GE4663@nille.org>
Reply-To: Nicklas af Ekenstam <nille@comology.se>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.3.28i
X-Operating-System: Linux parker 2.4.10-4GB
X-PGP-Key: http://nille.org/pubkey.txt
Organization: comology.se
Subject: Reg exp to remove line feeds before lines starting with white space
From: nille@comology.se (Nicklas Af Ekenstam)
Hi
Im trying to do some parsing of mail headers and since not all
headers follow the pattern:
';
$lines = split("\n", $mem);
for ( $c = count($lines), $i=0, $headers = array(), $lastkey = 'x'
;$i<$c
;$i++)
{
$t = $lines[$i];
if (preg_match('/^([a-z].+?):\s*(.+)/i', $t, $result))
{
$lastkey = $result[1];
$headers[$lastkey] = trim($result[2]);
}
else
{
$headers[$lastkey] .= "\n" . $t;
}
// end of headers?
if (trim($t)=='')
break;
}
while (list($key, $val) = each($headers))
echo "$key=$val\n";
?>
note: i have renamed headers with same name! like 'received' cause I don't
know how you want to handle them!
good luck,
//elias
"Nicklas Af Ekenstam" <nille@comology.se> wrote in message
news:20020725103627.GE4663@nille.org...
> Hi
>
> I'm trying to do some parsing of mail headers and since not all
> headers follow the pattern:
>
> X-Header-A: A string
> X-Header-B: Another string that is longer
> X-Header-C: And this string is very very long compared
>
> But may very well look like this:
>
> X-Header-A: A string
> X-Header-B: Another string that is
> longer
> X-Header-C: And this string is very very long
> compared
>
> (Note that two of the header lines above wrap more than one line.)
>
> In order to read the headers into an array of header key/value fields
> i would like a reg exp to remove all new lines/line feeds before a
> line starting with one or more white spaces or tabs.
>
> I've tried myself but I'm just nog good with reg exps.
>
>
> Thanks in advance,
> Nicklas