【问题标题】:PHP heredoc is not working when heredoc's string from MySQL text Type当 MySQL 文本类型中的 heredoc 字符串时,PHP heredoc 不起作用
【发布时间】:2020-12-01 17:57:31
【问题描述】:

我将 heredoc 存储在 MySQL 文本类型中。

Hi, $candidate.
This is $interviewer speaking.

我想像下面这样动态地使用heredoc。 (“$mailTemplate->body”是上面 MySQL 文本类型的字符串。)

$candidate = 'CANDIDATE';
$interviewer = 'INTERVIEWER';

$mailBody = <<< EOM
$mailTemplate->body
EOM;

但是 heredoc 不起作用,变量按原样输出。

Hi, $candidate. This is $interviewer speaking.

有什么想法吗?还是不可能?

谢谢。

【问题讨论】:

    标签: php mysql heredoc


    【解决方案1】:

    不,你不能这样。 $mailTemplate-&gt;body 中的 $... 被视为文本。

    但你可以使用sprintf。

    $text = "Hello %s this is a %s.";
    
    $valueA = 'World';
    $valueB = 'test';
    
    echo sprintf(
        $text,
        $valueA,
        $valueB
    );
    

    工作example。

    输出

    Hello World this is a test.
    

    将字符串中的所有变量替换为%s,并将值作为sprintf 中的参数提供。

    【讨论】:

    • 你节省了我的时间。感谢您的出色回答和支持!
    • 不客气 :) 请随时 accept the answer 如果它在点。
    猜你喜欢
    • 2014-10-04
    • 2011-11-26
    • 2013-12-06
    • 2016-08-24
    • 2011-04-02
    • 2019-11-21
    • 2019-04-07
    • 2021-03-23
    相关资源
    最近更新 更多