SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: str_replace

  1. #1
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace

    Can someone help me use this to pretty much open a file, parse the data and put it into a variable, change one tag of the variable, and then rewrite the file with the same information as the variable.

    Here is what I have:

    PHP Code:
       $postsname "includes/posts.txt";
       
    $postsfile fopen($postsname"r");
       
    $postdata fread($postsfilefilesize($postsname)); 
       
    $new_postdata str_replace("<!-- End of Post -->""Replace Text<!--End of Post -->"$postdata);
       
    fclose($postsfile);
       
    $postsfile_write fopen($postsname"w");
       
    fwrite($postsfile_write$new_postdata);
       
    fclose($postsfile_write); 
    Thanks for your help.

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At first glance that code looks like it should work... do you have a question about one part of it, is there something you want to add to it, or is it not working?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry about that...I have it now.

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that looks like it is working except for that is is writting data onto the end of the file rather than replacing the whole file with the new string.

    'w' is the correct string mode for fopen, I'm not spotting why it isn't working. I'll take another look later, I have to scram at the moment, though.

    edit: Ah, I see. Glad it works!
    Last edited by samsm; May 20, 2003 at 16:05.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


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
  •