如何解决当MySQL文本类型中的Heredoc字符串时,PHP Heredoc不起作用 输出
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
中将这些值作为参数提供。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。