Help with variables on a string retrieved from a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Corbenik
Forum Newbie
Posts: 2
Joined: Fri Feb 29, 2008 6:56 pm

Help with variables on a string retrieved from a file

Post by Corbenik »

Hello, I'm having some problems with a project I have. I looked around but didn't find something like this, though to be fair I didn't look that hard :P If someone
has posted a similar problem please point me in the direction of the post.
Well, basically I want to make a service where I recieve a set of values to fill a form. The form would be in a text file on my server and depending on the
form the user wants to fill the service would read a specific text file. My idea was to have those files prepared for this by inserting the variable names in them.
For example, lets say I had a form where I wanted to put the name of an employee, the form file on the server would start with something like this:

YYYY-MM-DD

Mr. $name
Random text with financial stuff I don't understand
.
.
.


So, I have that text file on the server and I have read it with file_get_contents(), which I understand returns a string, but when I print that string it literally says "$name", instead of the value I sent as a parameter when calling my service. If I concatenate the string: " $name " at the end this string the mention of the variable does get replaced with the value I want, so I don't know what I'm doing wrong. Does the string returned by file_get_contents() have a special format which makes the interpreter overlook mentions of variable names? Thanks in advance, I hope someone can help me with this.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Help with variables on a string retrieved from a file

Post by alex.barylski »

I only glanced at your question but it sounds like your using file_get_contents to return a template of sorts and are curious as to why the $fname variables are still intact and not the values you set them in PHP???

Code: Select all

$fname = 'My First Name';
 
$buffer = file_get_contents('somefile.dat');
 
$buffer = str_replace('$fname', $fname, $buffer);
I am not sure I would recommend using $fname though -- it's common practice to wrap each placeholder in a double % so something like:
Hello %%FNAME%%, how are you today???
The code would be:

Code: Select all

 
$fname = 'My First Name';
 
$buffer = file_get_contents('somefile.dat');
 
$buffer = str_replace('%%FNAME%%', $fname, $buffer);
This way you avoid any likelyhood of accidentally having the placeholder $fname represent something literal and having it replaced by accident.

HTH

Cheers :)
Corbenik
Forum Newbie
Posts: 2
Joined: Fri Feb 29, 2008 6:56 pm

Re: Help with variables on a string retrieved from a file

Post by Corbenik »

Yes, that's what I wanted to do and your solution gave me the exact result I needed. Thanks! :mrgreen:
Post Reply