Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:47:48+00:00 2026-05-27T18:47:48+00:00

I am having trouble reading in a file and using it as a template

  • 0

I am having trouble reading in a file and using it as a template in PHP. A portion of my code is pasted below. For some reason it never finds $$USER_NAME$$ in the string so it never replaces. I also pasted the beginning part of my template below.

$filename = "email/templates/welcome.txt";
$file = fopen($filename, "r")   
$body = fread($file, filesize($filename));
fclose($file);
$body = str_replace("$$USER_NAME$$", "John Doe", $body);
print($body);

Template:

<p>
   <img src="emailPic" alt="Logo" />
</p>
<p>Dear $$USER_NAME$$,</p>
<p>Thank you for registering to attend $$EVENT_NAME$$. We look forward to helping you reach your networking goals at $$EVENT_SHORT_NAME$$.
</p>...

Any ideas about why it wouldn’t find $$USER_NAME$$ in the template?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:47:49+00:00Added an answer on May 27, 2026 at 6:47 pm

    Try with single quotes:

    $body = str_replace('$$USER_NAME$$', 'John Doe', $body);
    

    otherwise the $ signs get interpreted by PHP (just try to do echo "$$USER_NAME$$" to understand that PHP will interpret it as echo $$USER_NAME."$$" or something similar).

    You can as well resolve the problem by escaping the $ if you still want to use double quotes:

    $body = str_replace("\$\$USER_NAME\$\$", "John Doe", $body);
    

    Check the PHP manual page on Strings to know what are the differences between single quoted and double quoted strings in PHP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble reading data from a file into a vector of Orders.
I'm having trouble in reading csv file that are having alphanumeric data. below is
I'm having trouble reading a compressed (deflated) data file using C# .NET DeflateStream(..., CompressionMode.Decompress)
I am having some trouble with my PHP code. Basically I have a list
I'm having trouble reading an entire specific line of a text file using Python.
I'm having trouble reading in supplementary unicode characters using Java. I have a file
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I'm having trouble reading and writing QByteArray data to a file. My goal is
I'm having trouble reading a chunked response when using a StreamReader to read the
I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.