0

This function:

public function createMessage($post, $messagename) { $dbData = array(); // don't forget to initialize your array foreach ($post as $key => $value) { $sanitizedValue = strip_tags(ucfirst(strtolower($value))); $message = str_replace('{$'.$key.'}', $sanitizedValue, file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/english/forms/_includes/_emails/' . $messagename . '.html')); } return $message; } 

doesn't replace the variable names (which are {$var} in the copy) in the HTML file when I use file_get_contents, but if I just use a string instead of the file_get_contents function it works. Anyone have any thoughts. I didn't see anything on PHP.net's documentation that helped.?

1
  • It reaches the right directory, it just doesn't do the str_replace. Commented Dec 28, 2010 at 23:15

3 Answers 3

1

You're re-loading the file for each variable. Move the file_get_contents call before the loop:

public function createMessage($post, $messagename) { $dbData = array(); // don't forget to initialize your array $message = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/english/forms/_includes/_emails/' . $messagename . '.html'; foreach ($post as $key => $value) { $sanitizedValue = strip_tags(ucfirst(strtolower($value))); $message = str_replace('{$'.$key.'}', $sanitizedValue, $message)); } return $message; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Well even if it was you wouldnt be getting the result you expect... since you open the content in each iteration of the loop but never save the content back you will never replace all the variables successfully. You will only replace the one form the last iteration. Im goign to guess is that whatever the last $key is in $_POST doesnt have a variable matching it in the message template... so it seems like nothing is working even though it really is.

You need to use file_get_contents outside the loop:

 $message = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/english/forms/_includes/_emails/' . $messagename . '.html'; foreach ($post as $key => $value) { $sanitizedValue = strip_tags(ucfirst(strtolower($value))); $message = str_replace('{$'.$key.'}', $sanitizedValue, $message)); } 

Comments

0

Try printing the output of $_SERVER['DOCUMENT_ROOT'] . '/english/forms/_includes/_emails/' . $messagename . '.html' to be sure you're reaching the correct directory.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.