当MySQL文本类型中的Heredoc字符串时,PHP Heredoc不起作用 输出

问题描述

我将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.

有什么主意吗?还是不可能?

谢谢。

解决方法

不,您不能这样做。 const sendMail = async () => { const mail = await new MailComposer({ to: ...,from: ...,subject: ...,html: ...,}); const message = await mail.compile().build(); const encodedMessage = message .toString('base64') .replace(/\+/g,'-') .replace(/\//g,'_') .replace(/=+$/,''); await gmail.users.messages.send({ userId: 'me',requestBody: { raw: encodedMessage },}); }中的$...被视为文本。

但是您可以使用sprintf

$mailTemplate->body

工作example

输出

$text = "Hello %s this is a %s.";

$valueA = 'World';
$valueB = 'test';

echo sprintf(
    $text,$valueA,$valueB
);

Hello World this is a test. 替换字符串中的所有变量,并在%s中将这些值作为参数提供。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...