Reputation: 5568
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.?
Upvotes: 0
Views: 1265
Reputation: 60413
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));
}
Upvotes: 1
Reputation: 34107
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;
}
Upvotes: 1
Reputation: 11210
Try printing the output of $_SERVER['DOCUMENT_ROOT'] . '/english/forms/_includes/_emails/' . $messagename . '.html'
to be sure you're reaching the correct directory.
Upvotes: 0