On Thu, 26 Jul 2001 15:24, Steven Michaels wrote:
>> ----- Original Message -----
> From: "Don Read" <[EMAIL PROTECTED]>
> To: "Steven Michaels" <[EMAIL PROTECTED]>
> Cc: <[EMAIL PROTECTED]>
> Sent: Wednesday, July 25, 2001 1:11 PM
> Subject: RE: [PHP] Perl Splice Equivalent
>
> > On 25-Jul-2001 Steven Michaels wrote:
> > > Hello,
> > > I've been wanting to open a file, read its contents,
> > > and if one line matches my text, have the program
> > > remove that line.  The file is in this format (a common
> > > text file.):
> > > User1
> > > User2
> > > User3
> > > What I want the program to do is to go through each
> > > line, and if that line matches User2, then have it
> > > delete it, so the finished text file would be
> > > User1
> > > User3
> > > The problem is that I can't find an equivalent of
> > > splice in perl, and that was what I used before
> > > to do this.  Could someone help me out?
> > > Thanks!
> >
> > PHP4:
> >   array_splice()
> >
> > PHP3: untested stab:
> >   unset($user[1]);
> >   unset($user['User2']);
> >
> >
> > 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.

> What would be the actual code for this however in
> PHP4?  I'm new to the language, but know moderate
> amounts of PERL, so I'm really struggling with how
> I can assign a file's contents into an array and
> delete a line, then write the edited version back
> to the file.  Also, the line now contains more
> data.  To clarify what I need code for:
> Get every line from file.txt which is in this format:
> User1~background~font~
> Username2~background~font~
> Login3~background~font~
> Then, match the first field (The one before the ~)
> to Username2.  If it does match, have the program
> get rid of the entire line.  Then write everything back
> to file.txt.
> Sorry if I'm unclear, its really hard to explain what
> I need done in text.
> Thanks!!
> Steven Michaels
> http://hollow.myip.org/

Excuse me reordering the message, but it's easier to read when read 
downwards chronologically.

To read a file into an array, where each line of the file is an array 
element, just use file() See the docs on filesystem functions for more 
info.

Then you loop through the array (see the array handling functions) and 
perhaps use substr (see string functions) to check whwether the text you 
want is in the current array element. You may need a regex, but from your 
information above I would think substr would do the job. Or maybe you 
could explode the array element on the ~ and just check the first element 
of the exploded array for the value required.

Then, depending on whether you want to keep the current line or not, you 
could write it to a new array (which will be the basis of what you write 
out to file).

Or maybe I'm not quite understanding you - are all those fields in one 
row, or...
-- 
David Robley      Techno-JoaT, Web Maintainer, Mail List Admin, etc
CENTRE FOR INJURY STUDIES      Flinders University, SOUTH AUSTRALIA  

   The world is coming to an end. Please log off.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to