This isn't tested, but should work :)

//////////////////////////////////////////////////

$to = '';
$subject = '';
$message = '';

$matches = array();

$data = file( $file );
foreach( $data as $id => $line )
{
    if( ereg( '^<TO>(.*)$', $line, $matches ) )
    {
        $to = trim( $matches[1] );
        unset( $data[$id] );
    }
    else
    if( ereg( '^<FROM>(.*)$', $line, $matches ) )
    {
        $from = trim( $matches[1] );
        unset( $data[$id] );
    }
}

$message = implode( '', $data );

//////////////////////////////////////////////////

Cheers,
Rob.


On Tue, 2003-09-16 at 11:46, Susan Ator wrote:
> 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
> 
> 
-- 
.---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org   |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the  |
| stuff of nightmares grasp for your soul.    |
`---------------------------------------------'

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

Reply via email to