Re: [PHP] How do I do this PERL in PHP?

2003-09-17 Thread Don Read

On 16-Sep-2003 Susan Ator wrote:
> I have a text file with the following format:
> 
>  name
>  stuff
> 
> message text
> message text
> message text
> 



If you're sure the format is exactly that, then ...

$pat=array(
  '',
  '',
);

$marker='';

$data=file_get_contents('dafile.txt');

$msgblks=explode('', $data);

foreach($msgblks as $blk) {
  list($to, $subj, $msg) = 
explode($marker,preg_replace($pat, $marker, $blk));
  ...
  do__your_stuff($to, $subj, $msg);
  ...
}


Regards,
-- 
Don Read [EMAIL PROTECTED]
-- It's always darkest before the dawn. So if you are going to 
   steal the neighbor's newspaper, that's the time to do it.

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



RE: [PHP] How do I do this PERL in PHP?

2003-09-16 Thread SLanger
hello

just a side note

instead of 
> ereg( '^<$var>(.*)$', $line, $matches )
use 
preg_match('/^<'.$var.'>(.*)$/', $line, $matches)
since it is faster than ereg. 

Regards
Stefan Langer

RE: [PHP] How do I do this PERL in PHP?

2003-09-16 Thread Susan Ator
Thank y'all so much for all your help.

sa

-Original Message-
From: Robert Cummings [mailto:[EMAIL PROTECTED]
Sent: Tuesday, September 16, 2003 2:30 PM
To: Susan Ator
Cc: [EMAIL PROTECTED]
Subject: RE: [PHP] How do I do this PERL in PHP?


The following will do variable matching like you asked:

if( ereg( '^<'.$var.'>(.*)$', $line, $matches ) )

Cheers,
Rob.


On Tue, 2003-09-16 at 13:34, Susan Ator wrote:
> It works beautifully. My only question is there a way to pass a variable
to
> the ereg? In other words:
> 
> if( ereg( '^(.*)$', $line, $matches ) )
> 
> would become:
> 
> if( ereg( '^<$var>(.*)$', $line, $matches ) )
> 
> I've tried doing eval but that was a no go.
> 
> I have a lot of fields and I hate doing the same thing over and over if
> there is a way to avoid it. :)
> 
> sa
> 
> 
> -Original Message-
> From: Robert Cummings [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, September 16, 2003 12:05 PM
> To: Susan Ator
> Cc: [EMAIL PROTECTED]
> Subject: Re: [PHP] How do I do this PERL in PHP?
> 
> 
> This isn't tested, but should work :)
> 
> //
> 
> $to = '';
> $subject = '';
> $message = '';
> 
> $matches = array();
> 
> $data = file( $file );
> foreach( $data as $id => $line )
> {
> if( ereg( '^(.*)$', $line, $matches ) )
> {
> $to = trim( $matches[1] );
> unset( $data[$id] );
> }
> else
> if( ereg( '^(.*)$', $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:
> > 
> >  name
> >  stuff
> > 
> > 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 ";
> > $subject = "everything to the left of ";
> > $message = "everything to the left of ";
> > 
> > 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 = )) {
> >   chomp $line;
> >   field_found($line,"",\$to);
> >   field_found($line,"",\$subject);
> >   field_found($line,"",\$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
> 
-- 
.-.
| 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

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



RE: [PHP] How do I do this PERL in PHP?

2003-09-16 Thread Robert Cummings
The following will do variable matching like you asked:

if( ereg( '^<'.$var.'>(.*)$', $line, $matches ) )

Cheers,
Rob.


On Tue, 2003-09-16 at 13:34, Susan Ator wrote:
> It works beautifully. My only question is there a way to pass a variable to
> the ereg? In other words:
> 
> if( ereg( '^(.*)$', $line, $matches ) )
> 
> would become:
> 
> if( ereg( '^<$var>(.*)$', $line, $matches ) )
> 
> I've tried doing eval but that was a no go.
> 
> I have a lot of fields and I hate doing the same thing over and over if
> there is a way to avoid it. :)
> 
> sa
> 
> 
> -Original Message-
> From: Robert Cummings [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, September 16, 2003 12:05 PM
> To: Susan Ator
> Cc: [EMAIL PROTECTED]
> Subject: Re: [PHP] How do I do this PERL in PHP?
> 
> 
> This isn't tested, but should work :)
> 
> //
> 
> $to = '';
> $subject = '';
> $message = '';
> 
> $matches = array();
> 
> $data = file( $file );
> foreach( $data as $id => $line )
> {
> if( ereg( '^(.*)$', $line, $matches ) )
> {
> $to = trim( $matches[1] );
> unset( $data[$id] );
> }
> else
> if( ereg( '^(.*)$', $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:
> > 
> >  name
> >  stuff
> > 
> > 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 ";
> > $subject = "everything to the left of ";
> > $message = "everything to the left of ";
> > 
> > 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 = )) {
> >   chomp $line;
> >   field_found($line,"",\$to);
> >   field_found($line,"",\$subject);
> >   field_found($line,"",\$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
> 
-- 
.-.
| 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



RE: [PHP] How do I do this PERL in PHP?

2003-09-16 Thread Chris Shiflett
--- Susan Ator <[EMAIL PROTECTED]> wrote:
> It works beautifully. My only question is there a way to pass a
> variable to the ereg? In other words:
> 
> if( ereg( '^(.*)$', $line, $matches ) )
> 
> would become:
> 
> if( ereg( '^<$var>(.*)$', $line, $matches ) )
> 
> I've tried doing eval but that was a no go.

That first argument is just a string, so you can generate it any way you want.
When you use single quotes, the string is considered to be literal text. Use
double quotes if you want $var to be evaluated.

Hope that helps.

Chris

=
Become a better Web developer with the HTTP Developer's Handbook
http://httphandbook.org/

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



RE: [PHP] How do I do this PERL in PHP?

2003-09-16 Thread Susan Ator
It works beautifully. My only question is there a way to pass a variable to
the ereg? In other words:

if( ereg( '^(.*)$', $line, $matches ) )

would become:

if( ereg( '^<$var>(.*)$', $line, $matches ) )

I've tried doing eval but that was a no go.

I have a lot of fields and I hate doing the same thing over and over if
there is a way to avoid it. :)

sa


-Original Message-
From: Robert Cummings [mailto:[EMAIL PROTECTED]
Sent: Tuesday, September 16, 2003 12:05 PM
To: Susan Ator
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] How do I do this PERL in PHP?


This isn't tested, but should work :)

//

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

$matches = array();

$data = file( $file );
foreach( $data as $id => $line )
{
if( ereg( '^(.*)$', $line, $matches ) )
{
$to = trim( $matches[1] );
unset( $data[$id] );
}
else
if( ereg( '^(.*)$', $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:
> 
>  name
>  stuff
> 
> 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 ";
> $subject = "everything to the left of ";
> $message = "everything to the left of ";
> 
> 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 = )) {
>   chomp $line;
>   field_found($line,"",\$to);
>   field_found($line,"",\$subject);
>   field_found($line,"",\$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

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



Re: [PHP] How do I do this PERL in PHP?

2003-09-16 Thread Robert Cummings
This isn't tested, but should work :)

//

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

$matches = array();

$data = file( $file );
foreach( $data as $id => $line )
{
if( ereg( '^(.*)$', $line, $matches ) )
{
$to = trim( $matches[1] );
unset( $data[$id] );
}
else
if( ereg( '^(.*)$', $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:
> 
>  name
>  stuff
> 
> 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 ";
> $subject = "everything to the left of ";
> $message = "everything to the left of ";
> 
> 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 = )) {
>   chomp $line;
>   field_found($line,"",\$to);
>   field_found($line,"",\$subject);
>   field_found($line,"",\$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



[PHP] How do I do this PERL in PHP?

2003-09-16 Thread Susan Ator
I have a text file with the following format:

 name
 stuff

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 ";
$subject = "everything to the left of ";
$message = "everything to the left of ";

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 = )) {
  chomp $line;
  field_found($line,"",\$to);
  field_found($line,"",\$subject);
  field_found($line,"",\$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