在PHP中,如何创建一个没有转义引号的多行变量? [英] In PHP, how do I create a multi-line variable without escaping quotes?

查看:271
本文介绍了在PHP中,如何创建一个没有转义引号的多行变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CFML中有以下结构:

In CFML there is the construct:

<cfsavecontent name="HTML_content">
    <div class="test_div" style="color:#FFF;">Test</div>
</cfsavecontent>

在PHP中,似乎没有办法做到这一点没有一些真正hacky函数或转义双引号。我试图通过保存电子邮件内容并简单地邮寄它在同一页面中创建电子邮件(大型html文档),但似乎没有办法在PHP中这样做,而不使用 $ var =\\ \\bad code\;

Within PHP there seems to be no way of doing this without some really hacky function or escaping double quotes. I'm trying to create emails (large html documents) within the same page by saving the email content and simply mailing it but it seems like there's no way of doing this within PHP without using $var = "\"bad code\"";

有人能指出正确的方向吗?

Can somebody point me in the right direction?

我试过下面的 deceze答案

$var = <<<HTML
    <div class="test_div" style="color:#FFF;">Test</div>
HTML;

但我得到以下错误:

Parse error: syntax error, unexpected $end, expecting T_VARIABLE or T_END_HEREDOC or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN in C:\vhosts\jpl\pages\about\testimonials.php on line 29







HTML;


For anyone else that is stuck on this, The HTML; part at the end of the HEREDOCS example needs to be right at the beginning (no tabs, spaces or anything) of the end.

因此 newline > HTML; 换行符 [tab或space] HTML;

推荐答案

$var = '<div class="test_div" style="color:#FFF;">Test</div>';

$var = <<<HTML
    <div class="test_div" style="color:#FFF;">Test</div>
HTML;

有很多方法可以引用字符串... http://php.net/manual/en/language.types.string.php

So many ways to quote a string... http://php.net/manual/en/language.types.string.php

也请考虑将电子邮件内容放入单独的文件中,并在需要时包含

Also consider putting the email contents into a separate file and includeing it where needed.

这篇关于在PHP中,如何创建一个没有转义引号的多行变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆