replacing/deletes text to a specific line

Discussion in 'PHP' started by spaceman12, Mar 10, 2011.

  1. #1
    The title says it all,..well as all u can see, there are many methods to accomplish this done but not before undergoing through a heavy tasking process. For instance,
    1. Open the file
    2. Reads the content of the file into array.
    3. Find out the desire line of text that we want to replace.
    4. Make that line of text as variable and assigns with the new data(that we want to replace) to its values.
    5.open another file.
    6.Write the whole contents to the file opened in 5.
    7. Put the contents back to the file of original.

    Can anyone contributes a better way to get this done? Like replace/delete a specific line without affecting other characters or anything therein?
    Thanx.

    P.S
    reading and using str_replace to make the alteration always ends up in giving some kind of result beyond desired after the first round of code execution.
     
    spaceman12, Mar 10, 2011 IP
  2. kajfat

    kajfat Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $pieces = explode("\\n", $text);
    foreach($pieces) {
    if(desired text){
    $new_desired_line = str_replace(....);
    }
    $out.=line;
    }

    write $out to another file.

    :)
     
    kajfat, Mar 10, 2011 IP