Replacing something in a file (fwrite)

Discussion in 'PHP' started by blueparukia, Dec 9, 2007.

  1. #1
    Basically what I want to do is a find and replace operation, so I retrieve the contents of a file and replace .quote with nothing, my current code:

    
    <?php
    
    function comment_code(){
    $css = "test.css";
    chmod($css, 0777);
    $handle = fopen($css, 'w');
    $style = file_get_contents($css);
    $newstyle = str_replace(".quote", "nothing1", $style);
    fwrite($handle,$newstyle);
    echo $newstyle;
    };
    
    comment_code();
    
    ?>
    PHP:
    All thats doing is wiping the file. It also wipes the file if I use this:

    
    <?php
    
    function comment_code(){
    $css = "test.css";
    chmod($css, 0777);
    $handle = fopen($css, 'w');
    $style = file_get_contents($css);
    echo $style;
    };
    
    comment_code();
    
    ?>
    PHP:

    Could someone please help,

    BP
     
    blueparukia, Dec 9, 2007 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You are calling it in wrong order.
    Do first
    $style = file_get_contents($css);
    and after that
    $handle = fopen($css, 'w');
     
    hogan_h, Dec 9, 2007 IP
    blueparukia likes this.
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    That was too simple. :p

    Thanks, rep given,

    BP
     
    blueparukia, Dec 9, 2007 IP
    hogan_h likes this.