Replacing variables while writing data files?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Mousel
    Senior Member
    • May 2000
    • 281
    • 5.0.0

    Replacing variables while writing data files?

    Hello,

    I'm reading a file on my server and then I'm writing that file out to a different directory. In the original file there is a tag <-- $username -->. In the new file that is written I'd like all occurrences of <-- $username --> replaced with an actual username. Here is the script I'm using:

    PHP Code:
    $file_loc "/home/public_html/index.html";

    $whattoread fopen($file_loc"r");

    $file_contents fread($whattoreadfilesize($file_loc));

    fclose($whattoread);


    //new file to make.  This is the file I would like all occurrences of <-- $username --> to be replaced with the actual username.

    $newfile fopen"/home/public_html/affiliates/username.html""w");
        
    fwrite($newfile"$file_contents");

        
    fclose($newfile); 
    Any ideas would be appreciated,

    Tim Mousel
    Five Star Review Script - Add reviews to your website!
    Mixed Martial Arts - Houston MMA Training
    Women's Self-Defense - Courses and DVDs available
  • JohnM
    Senior Member
    • May 2000
    • 622

    #2
    PHP Code:
    $var str_replace('<!-- $username -->'$username$var); 

    Comment

    • Tim Mousel
      Senior Member
      • May 2000
      • 281
      • 5.0.0

      #3
      Hi,

      Thanks! I went to the manual and read about it....I hate to sound stupid, but where do I place that code?

      Thanks,

      Tim
      Five Star Review Script - Add reviews to your website!
      Mixed Martial Arts - Houston MMA Training
      Women's Self-Defense - Courses and DVDs available

      Comment

      • Tim Mousel
        Senior Member
        • May 2000
        • 281
        • 5.0.0

        #4
        I figured it out.

        Thanks for the info,

        Tim
        Five Star Review Script - Add reviews to your website!
        Mixed Martial Arts - Houston MMA Training
        Women's Self-Defense - Courses and DVDs available

        Comment

        • JohnM
          Senior Member
          • May 2000
          • 622

          #5
          PHP Code:
          $whattoread fopen($file_loc"r");

          $file_contents fread($whattoreadfilesize($file_loc));

          fclose($whattoread); 
          would be better written as:

          PHP Code:
          $file_contents implode("",file($file_loc)); 

          Comment

          • Tim Mousel
            Senior Member
            • May 2000
            • 281
            • 5.0.0

            #6
            Cool! I'll give it a try. I always appreciate these tips.

            Tim
            Five Star Review Script - Add reviews to your website!
            Mixed Martial Arts - Houston MMA Training
            Women's Self-Defense - Courses and DVDs available

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...
            😀
            😂
            🥰
            😘
            🤢
            😎
            😞
            😡
            👍
            👎