Add/Replace Line/Text in TextFiles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
magOO
Forum Newbie
Posts: 1
Joined: Fri Mar 26, 2004 3:39 am

Add/Replace Line/Text in TextFiles

Post by magOO »

Hello!
I need for my project a Function that Add or Replace Textstrings or a whole Line in a Textfile like the ModInstaller for phpBB.
It should able to do the following actions:
  • Add a Line Before another Line
    Add a Line After another Line
    Replace a Line
    Write String After a String in Line
    Write String Before a String in Line
    Replace String in Line
Is there a function out or how can i write a function that can do it?
Thanks for your help
magOO

Sorry for my bad English :oops:
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Add/Replace Line/Text in TextFiles

Post by TheBentinel.com »

You could split the text file into an array of lines, then you could do whatever you wanted with it.

Look into the split() and explode() functions.

http://php.net/split
http://php.net/explode

You'll split on the newline character ("\n") or perhaps on "\r\n", depending on your file. Try "\n" and if you get what you want you're in good shape.
Post Reply