0

I have a small app I am working on that generates configuration files. Because it may be used to generate a few different variants of configurations, I gather the variable values from the user, and then read in a template configuration from a file. That file has the names of the variables in it (i.e. $ip_address) at the proper positions. I have set those variables in my php code before reading and the printing out the files. However, the variable names are printed out, not the values of the variables:

$hostname = $_POST['username'] . "_891";
$domain = $_POST['username'] . ".local";   
$routerFile = "891.txt";

$file_handle = fopen($routerFile, "r");
while (!feof($file_handle)) {
   $line = fgets($file_handle);
   print $line . "<br />";
}

Output example:

hostname $hostname
ip domain-name $domain

How do I get php to replace the variables names with their stored values?

4
  • 1
    Show us the code where you create the file. Commented Sep 10, 2012 at 13:19
  • 1
    Wait, why are you doing it like this? Can you provide more context? I was about to provide a solution but the question doesn't make too much sense. What are your goals and restrictions? Commented Sep 10, 2012 at 13:22
  • Why does (what appears to be) a static text file have variables, for one thing? Why do you need to read it like this at all? Why is it not simply a PHP template file? Commented Sep 10, 2012 at 13:28
  • If you need to do it this way...Instead of using PHP variables in a static text file - use placeholders and then replace with the values of the variables. I typically use placeholders like %%HOSTNAME%% Commented Oct 6, 2012 at 6:05

3 Answers 3

2

I would go this way:

Create a template with hostname = <?php echo $hostname; ?> lines like so:

hostname <?php echo $hostname; ?>
ip domain-name <?php echo $domain; ?>

Then in PHP creating a template You would do:

ob_start();
include_once($routerFile);
$file_output = ob_get_clean();

Now You have the file template filled with the appropriate variables and You can save that file:

file_put_contents('my_new_router_file.txt', $file_output);

And You should be done.

Sign up to request clarification or add additional context in comments.

3 Comments

Shady, This solution is working fantastically. one of the goals was the keep the template file as plain text readable as possible (rather than filling it with echo's html tags, etc). The only issue I have with this is that it is removing any new lines, and presenting the entire file content as one line. Aside from inserting <br />'s at the end of every line (which I think might get stripped anyway) how could I get the newlines to come through?
Maybe there is something wrong with encoding or linebreaks... Or try to insert additional space after each ?> at the end of line and a linebreak (Enter) afterwards... If that won't help, try to leave one line empty between the two.
Incase any one stumbles on this in the future, I managed to solve the line break issue by enclosing the output in a html <textarea>. That actually worked better for my solution anyway.
2

You could use eval(), but that's not very safe, so I'd write a super simple regex replace.

$replaced = preg_replace_callback("/\b$\w+\b/", 
                                  function($match) use $hostname, $domain { 
                                      return $$match
                                  },
                                  $line);

However, I'd place the terms that need to be replaced inside of an associative array, otherwise your list of use will become a nightmare.

If you really want those files to be treated as PHP files and there is no security risk with them (they didn't come from an untrusted source) just include them.

6 Comments

I can only guess that this answer will be the user's first step into unmaintainable code (and probably more questions). If you look closely at the question, it's trying to do something very simple in a convoluted way. In regards to eval, "Not very safe" is an understatement. Completely out of the question is probably more like it.
@WesleyMurch I took a stab at guessing with the last paragraph, but I may be missing something very obvious. As for eval(), it's only unsafe if possibly tainted data can get there. As for a matter of style though, it's terrible, and there is almost always a better way (as I'm sure everybody already knows).
If this was my homework assignment and I had to abide by the restrictions I'd use the associative array approach with a str_replace.
I do not suppose that there are only two variables, but could be thousands of it... Then the use $variable part would become very "developer friendly"...
@WesleyMurch That would be a good option if the list of terms was known.
|
0

I second alex answer. Another possibility is to ditch the txt file, replace it with a php file and just include that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.