+ Reply to Thread
Results 1 to 2 of 2

Thread: Replace string on file

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10

    Replace string on file

    I want to create a script that replaces a string inside a file and then write it back , i am actually new to PHP so i'd be thankful if you gave me the snippet for it.
    Last edited by supercoder; 11-12-2012 at 02:27 AM.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    41
    Here is the code snippet about how to replace a string on a file then completely rewrite it back.
    Code:
    <?php
    //read the entire string
    $file=implode("\n",file('file.txt'));
    
    $fp=fopen('file.txt','w');
    //replace something in the file string - this is a VERY simple example
    $file=str_replace('basem','sayed',$file);
    
    //now, TOTALLY rewrite the file
    fwrite($fp,$file,strlen($file));
    ?>

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts