简体   繁体   English

在Heredoc标签之间连接内容

[英]Concatenating Content between Heredoc Tags

I am converting some formatted html text to a PDF file, and in doing so, I must place a table in the document. 我正在将某些格式的html文本转换为PDF文件,并且在这样做时,必须在文档中放置表格。 In order to put the table into the PDF correctly, I must use heredoc tags instead of regulars quotations (Came to this conclusion through some experimentation). 为了正确地将表格放入PDF中,我必须使用Heredoc标记而不是常规引号(通过一些实验得出的结论与此相同)。 What I am wondering is whether I can take some content from a POST call and place it between the heredoc tags a la: 我想知道的是,我是否可以从POST调用中获取一些内容,并将其放在heredoc标记la之间:

$table = $_POST['table'];
$html = <<<EOD . '$table' . EOD;

Or, something of the like. 或者,类似的东西。 I haven't come across a solution, and was wondering if this was even possible. 我没有找到解决方案,并且想知道这是否有可能。 I have tried passing the table variable directly to the method, but it is a fruitless effort. 我尝试将表变量直接传递给方法,但这是徒劳的。 It does work if passed with the heredoc tags, however. 但是,如果与heredoc标记一起传递,它将起作用。

You want to use an Encapsulated Heredoc for this purpose. 您要为此目的使用“ 封装的Heredoc ”。 Example #5 on that page. 该页面上的示例#5。 It allows you to place the heredoc in quotes and use curly braces to utilize your variables. 它允许您将heredoc放在引号中,并使用花括号将变量使用。

PHP Example PHP示例

$hey = 'Test 1 2 3';

$html = <<<"TEST"
Test me: {$hey}
TEST;

echo $html;

Returns 返回

Test me: Test 1 2 3

Yes, like the PHP documentation says: 是的,就像PHP文档说的那样:

$name = 'Davis';

echo <<<EOT
My name is "$name". I am printing some $_POST['table'].
Now, I am printing some {$foo->bar[1]} too!
This should print a capital 'A': \x41
EOT;

You have to make sure that your text is between your starting and ending line. 您必须确保您的文本起始行和结束行之间。

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

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