str_replace - Modify external file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jfcby

    #1

    str_replace - Modify external file?

    Hello,

    My data.txt file is formated like this:

    qqqq||eeee
    rrrr||tttt
    yyyy||uuuu
    iiii||oooo
    aaaa||bbbb

    I'm trying to get this code to replace the data I put in $name but it
    is not changing.

    How can the php code be changed to modify the data I put in $name?

    <?php
    $name = "aaaa"
    if(is_writable( "data.txt") )
    {
    $fp = fopen("data.txt ","a");
    $content = $fp;
    $rplName = str_replace("aa aa","TestRpl",$ content);
    fwrite($fp, $rplName);
    fclose($fp);
    }
    else
    {
    echo'File is not writable';
    }
    ?>

    Thank you for your help,
    jfcby
  • Robin

    #2
    Re: str_replace - Modify external file?

    jfcby wrote:
    Hello,
    >
    My data.txt file is formated like this:
    >
    qqqq||eeee
    rrrr||tttt
    yyyy||uuuu
    iiii||oooo
    aaaa||bbbb
    >
    I'm trying to get this code to replace the data I put in $name but it
    is not changing.
    >
    How can the php code be changed to modify the data I put in $name?
    >
    <?php
    $name = "aaaa"
    if(is_writable( "data.txt") )
    {
    $fp = fopen("data.txt ","a");
    $content = $fp;
    $rplName = str_replace("aa aa","TestRpl",$ content);
    fwrite($fp, $rplName);
    fclose($fp);
    }
    else
    {
    echo'File is not writable';
    }
    ?>
    >
    Thank you for your help,
    jfcby

    The first user contribution at

    does what you want.

    Robin

    Comment

    • Damodhar

      #3
      Re: str_replace - Modify external file?

      $filename = "data.txt";

      $handle = fopen($filename ,'r');
      $content = fread($handle,f ilesize($filena me));
      echo $content;
      $name = str_replace("aa a","TestRpl",$c ontent);
      fclose($handle) ;
      if ($name)
      {
      $handle = fopen($filename ,'w');
      fwrite($handle, $name);
      fclose($handle) ;

      }

      can u use it.... u want like this..

      Comment

      • jfcby

        #4
        Re: str_replace - Modify external file?

        Hello Robin,

        Thank you for the link below! I've been searching Google now for about
        3 weeks for a solution and did not find one.
        The first user contribution athttp://uk2.php.net/manual/en/function.fopen. php
        does what you want.
        Thank you for your help,
        jfcby

        Comment

        • jfcby

          #5
          Re: str_replace - Modify external file?

          Hello Damodhar,

          Thank you for the modified code.
          $filename = "data.txt";
          >
          $handle = fopen($filename ,'r');
          $content = fread($handle,f ilesize($filena me));
          echo $content;
          $name = str_replace("aa a","TestRpl",$c ontent);
          fclose($handle) ;
          if ($name)
          {
                  $handle = fopen($filename ,'w');
                  fwrite($handle, $name);
                  fclose($handle) ;
          >
          }
          But why did you say that:
          u want like this..
          Thank you for your help,
          jfcby

          Comment

          Working...