modifying a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonski
    New Member
    • Jun 2007
    • 53

    modifying a text file

    i've been trying to modify some lines from a text file... and i had this text file and a php script to modify it...

    //code.txt
    Code:
    101:2001
    102:2002
    103:2003
    104:2004
    //code_reader.php
    [PHP]<?php

    $code_file = 'code.txt';
    $code_file_line s = file($code_file );

    if(isset($_GET['address']) && isset($_GET['code'])){

    $address = $_GET['address'];
    $code = $_GET['code'];

    $c = 0;
    for($a = 0; $a<sizeof($code _file_lines); $a++){
    $content = explode(":", $code_file_line s[$a]);
    if($address == $content[0] && $code != $content[1]){
    $code_content[$c] = trim($content[0]).':'.trim($cod e);
    }else{
    $code_content[$c] = trim($content[0]).':'.trim($con tent[1]);
    }
    $c++;
    }

    $new_code = implode('\n', $code_content);
    file_put_conten ts($code_file, $new_code);
    }

    ?>
    [/PHP]

    now if i try to send to send values like this to modify a certain line...

    Code:
    code_reader.php?address=102&code=2001
    i want to output it as this...

    Code:
    101:2001
    102:2001
    103:2003
    104:2004
    but it turns out like this...

    Code:
    101:2001\n102:2001\n103:2003\n104:2004
    i know... using

    [PHP]file_put_conten ts($code_file, $new_code);[/PHP]

    will write it as string.. is there any function that will convert it to stream so that when it modifies the text file.. it can read "\n".


    thanks..

    bonski
  • bonski
    New Member
    • Jun 2007
    • 53

    #2
    hahaha... nevermind guys... i already found a solution.. using.. [PHP]chr(10)[/PHP] instead of '\n'.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, bonski.

      PHP won't parse '\n' when it's enclosed in single quotes. You need to use "\n" instead.

      Comment

      • bonski
        New Member
        • Jun 2007
        • 53

        #4
        Originally posted by pbmods
        Heya, bonski.

        PHP won't parse '\n' when it's enclosed in single quotes. You need to use "\n" instead.
        ei pbmods,

        ok.. thanks for the info... gotta try that now... hahaha..

        ^___^ bonski..

        Comment

        Working...