What am I doing wrong

Discussion in 'PHP' started by Mr.Bill, May 31, 2009.

  1. #1
    I have tried to make a small script that will read a txt file and replace certain words with a number and then display the final result.

    Its not working as I planned and if anyone know how to do this could I have it create a output.txt file with the all changes in it?

    Here is what I have so far.

    <?php
    
    $file = './3/test.txt';
    
    $words = array("United Kingdom", "United States", "Canada");
    $numbers = array("1", "2", "3");
    $content = '$file';
    $change = str_replace($words, $numbers, $content);
    Print $change;
    
    ?>
    PHP:
    I tried to use a line like this

    $phrase = fopen($file, "r");
    PHP:
    But then I get this displayed on the page
    So not sure if it is trying to work or not the txt file is 3.36mb in size
     
    Mr.Bill, May 31, 2009 IP
  2. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #2
    crap sorry had the wrong window open please delete this post
     
    Mr.Bill, May 31, 2009 IP
  3. web_doctor

    web_doctor Peon

    Messages:
    164
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use
    $phrase = fopen($file, "w");
    PHP:
    instead.

    then

    $content = $file;
    PHP:
    (qoute removed),
    I hope it helps.
     
    web_doctor, Jun 1, 2009 IP
  4. Ralle

    Ralle Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    I would do:
    <?php
    
    $file = './3/test.txt';
    
    $words = array("United Kingdom", "United States", "Canada");
    $numbers = array("1", "2", "3");
    $content = file_get_contents($file);
    $change = str_replace($words, $numbers, $content);
    print $change;
    
    ?>
    PHP:
    Many people say that fopen(), then fread() is faster than file_get_contents() but its easier to understand the latter.
     
    Ralle, Jun 1, 2009 IP