简体   繁体   English

使用file_get_contents()在html文件中设置php变量

[英]Setting php variable in html file using file_get_contents()

I have an automated email system set up to send an html file as an email. 我有一个自动电子邮件系统,可以将html文件发送为电子邮件。 I bring that file into my email with PHPMailer, using 我使用PHPMailer将该文件带入电子邮件

$mail->msgHTML(file_get_contents('mailContent.html'), dirname(__FILE__));

In the PHP source, before I add the mailContent.html, I have a variable $name='John Appleseed' (it is dynamic, this is just an example) 在PHP源代码中,在添加mailContent.html之前,我有一个$name='John Appleseed'变量(它是动态的,这只是一个示例)

In the HTML file, I'm wondering if there is a way that I can use this $name variable in a <p> tag. 在HTML文件中,我想知道是否有一种方法可以在<p>标记中使用此$name变量。

You can add a special string like %name% in your mailContent.html file, then you can replace this string with the value your want: 您可以在mailContent.html文件中添加一个特殊字符串,例如%name% ,然后可以使用所需的值替换此字符串:

In mailContent.html : mailContent.html

Hello %name%,
…

In your PHP code: 在您的PHP代码中:

$name='John Appleseed';

$content = str_replace('%name%', $name, file_get_contents('mailContent.html'));

$content will have the value Hello %name%, … , you can send it: $content的值为Hello %name%, … ,您可以发送它:

$mail->msgHTML($content, dirname(__FILE__));

You can also replace several strings in one call to str_replace() by using two arrays: 您还可以使用两个数组在一次对str_replace()的调用中替换多个字符串:

$content = str_replace(
    array('%name%', '%foo%'),
    array($name,    $foo),
    file_get_contents('mailContent.html')
);

Here is a function to do it using extract , and ob_* 这是一个使用extractob_*做到的功能

extract will turn keys into variables with their value of key in the array. extract将把键转换为变量,其值在数组中。 Hope that makes sense. 希望有道理。 It will turn array keys into variables. 它将把数组键变成变量。

function getHTMLWithDynamicVars(array $arr, $file)
{
    ob_start();

    extract($arr);

    include($file);

    $realData = ob_get_contents();

    ob_end_clean();

    return $realData;
}

Caller example: 呼叫者示例:

$htmlFile = getHTMLWithDynamicVars(['name' => $name], 'mailContent.html');

You need to use a templating system for this. 您需要为此使用模板系统。 Templating can be done with PHP itself by writing your HTML in a .php file like this: 可以使用PHP本身来进行模板化,方法是将HTML编写为.php文件,如下所示:

template.php: template.php:

<html>
<body>
    <p>
        Hi <?= $name ?>,
    </p>
    <p>
        This is an email message.  <?= $someVariable ?>
    </p>
</body>
</html>

Variables are added using <?= $variable ?> or <?php echo $variable ?> . 使用<?= $variable ?><?php echo $variable ?> Make sure your variables are properly escaped HTML using htmlspecialchars() if they come from user input. 如果变量来自用户输入,请确保使用htmlspecialchars()将变量正确地转义为HTML。

And then to the template in your program, do something like this: 然后对程序中的模板执行以下操作:

$name = 'John Appleseed';
$someVariable = 'Foo Bar';

ob_start();
include('template.php');
$message = ob_get_contents();
ob_end_clean();

$mail->msgHTML($message, dirname(__FILE__));

As well as using PHP for simple templating, you can use templating languages for PHP such as Twig . 除了使用PHP进行简单的模板制作外,您还可以使用PHP的模板语言(例如Twig)

In one of my older scripts I have a function which parses a file for variables that look like {{ variableName }} and have them replaced from an array lookup. 在我的一个较早的脚本中,我有一个函数可以解析文件,以查找看起来像{{ variableName }}变量,并从数组查找中替换它们。

function parseVars($emailFile, $vars) {
    $vSearch = preg_match_all('/{{.*?}}/', $emailFile, $vVariables);

    for ($i=0; $i < $vSearch; $i++) {
        $vVariables[0][$i] = str_replace(array('{{', '}}'), NULL, $vVariables[0][$i]);
    }

    if(count($vVariables[0]) == 0) {
        throw new TemplateException("There are no variables to replace.");
    }

    $formattedFile = '';
    foreach ($vVariables[0] as $value) {
        $formattedFile = str_replace('{{'.$value.'}}', $vars[$value], $formattedFile);
    }

    return $formattedFile;
}

Do you have the $name variable in the same file where you send the mail? 在发送邮件的同一文件中是否有$name变量? Then you can just replace it with a placeholder; 然后,您可以将其替换为占位符;

Place __NAME__ in the HTML file where you want the name to show up, then use str_ireplace('__NAME__', $name, file_get_contents('mailContent.html')) to replace the placeholder with the $name variable. __NAME__放置在要显示名称的HTML文件中,然后使用str_ireplace('__NAME__', $name, file_get_contents('mailContent.html'))将占位符替换为$name变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM