Portál AbcLinuxu, 10. prosince 2025 06:02
$posts= preg_split('/From (.+) na (.+) (Mon|Tue|Wed|Thu|Fri|Sat|Sun) (.{3}) (.{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})/', implode(file('test.txt')), -1, PREG_SPLIT_NO_EMPTY);
while(list($key,$value)= each($posts)){
$post_split= preg_split('/Message-ID: (.+)/',$value);
$head= $post_split[0];
$message_text= $post_split[1];
preg_match('/From: (.+) na (.+) \((.+)\)/',$head,$from);
preg_match('/Subject: (.+)/',$head,$message_subject);
$post= $from[0]."\n".$message_subject[0]."\n\n".trim($message_text)."\n";
echo "$key\n<br>".nl2br(htmlspecialchars($post))."<br>\n<hr>\n";
}
Potrebuju nacist soubor a ten pak naparsovat na castiNic vic, nic min to nedela. Ja jsem jen potreboval ten kod vylepsit aby byl efektivnejsi
#!/usr/bin/env perl
use strict;
use warnings;
my $msg={};
my $header='';
my $firstFrom = qr(^From (.+) na (.+) (Mon|Tue|Wed|Thu|Fri|Sat|Sun) (.{3}) (.{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$);
my $headerLine = qr(^\S+:);
sub ParseHeader {
my @headerLines = split m($/), $_[0];
my $ret = {};
@$ret{qw(usr site dayOfWeek month day hour min sec year)} =
shift(@headerLines) =~ /$firstFrom/;
foreach my $line (@headerLines) {
# tady si udelej co chces se zbytkem hlavicky a nacpi do $ret
}
return $ret;
}
sub PrintMsg {
my $msg = shift;
return unless ref $msg eq 'HASH' and %$msg;
use Data::Dumper;
print Data::Dumper->Dump([$msg],['msg']);
}
while (<>) {
chomp;
?$firstFrom?o and $header = $_ and next;
if ($header) {
/$headerLine/o and $header .= "$/$_" and next;
if (/^\s*$/) {
PrintMsg ($msg); # end of header - print msg
$msg = ParseHeader($header);
$header = '';
reset;
next;
}
# falesna hlavicka
$msg->{content} .= "$/" . $header;
$header = '';
next;
}
%$msg and $msg->{content} .= "$_$/" and next;
/\S/ and die 'Header not found';
}
#!/usr/bin/env perl
use strict;
use warnings;
my $msg={};
my @header;
my $firstFrom = qr(^From (.+) na (.+) (Mon|Tue|Wed|Thu|Fri|Sat|Sun) (.{3}) (.{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$);
my $headerLine = qr(^\S+:);
sub ParseHeader {
my $ret = {};
@$ret{qw(usr site dayOfWeek month day hour min sec year)} =
shift() =~ /$firstFrom/;
foreach my $line (@_) {
# tady si udelej co chces se zbytkem hlavicky a nacpi do $ret
}
return $ret;
}
sub PrintMsg {
my $msg = shift;
return unless ref $msg eq 'HASH' and %$msg;
use Data::Dumper;
shift @{$msg->{content}} while $msg->{content}[0] =~ /^\s*$/;
pop @{$msg->{content}} while $msg->{content}[$#{$msg->{content}}] =~ /^\s*$/;
print Data::Dumper->Dump([$msg],['msg']);
}
while (<>) {
chomp;
?$firstFrom?o and push @header, $_ and next;
if (@header) {
/$headerLine/o and push @header, $_ and next;
if (/^\s*$/) {
PrintMsg ($msg); # end of header - print msg
$msg = ParseHeader(@header);
@header = ();
reset;
next;
}
# falesna hlavicka
push @{$msg->{content}}, @header;
@header = ();
next;
}
%$msg and push @{$msg->{content}}, $_ and next;
/\S/ and die 'Header not found';
}
PrintMsg($msg);
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.