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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to