How do I do this PERL in PHP?
| From: | Susan Ator | Date: | Tue, 16 Sep 2003 15:46:27 +0000 |
| Subject: | How do I do this PERL in PHP? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-162758@lists.php.net to get a copy of this message | ||
I have a text file with the following format:
<TO> name
<SUBJECT> stuff
<MESSAGE>
message text
message text
message text
I need to be able to assign a variable to the data like so:
$to = "everything to the left of <TO>";
$subject = "everything to the left of <SUBJECT>";
$message = "everything to the left of <MESSAGE>";
so
$to = "name";
$subject = "stuff";
$message = "message text\nmessage text\nmessage text\n";
This is how it's being done in perl:
open(INPUT, $file) or die "couldn't open $file for reading: $!\n";
while (defined (my $line = <INPUT>)) {
chomp $line;
field_found($line,"<TO>",\$to);
field_found($line,"<SUBJECT>",\$subject);
field_found($line,"<MESSAGE>",\$message);
}
sub field_found(@_) {
my $line = shift;
my $fld = shift;
my $val = shift;
my $pos = index($line,$fld);
if ($pos == 0) { # found field
my $flen = length $fld;
my $llen = length $line;
$$val = substr($line,$flen,$llen);
} # found field
}
How in the world can I translate this to php?
sa