如何将代码收集到一个变量中

时间:2012-04-10 21:42:39

标签: php

我需要能够将所有这些信息(作为文本)放入变量$ all中,以便稍后在我的脚本中使用它。但是当我以后回复$ all时它不起作用。并且没有人对字体标签的使用有任何说法,我对此感到沮丧。

   $all = <<< STOPTHISCRAZYTHING
    echo "<br><br><textarea rows=\"30\" cols = \"100\">";
    echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
    foreach($same as $match)
        {
        echo "<img src=\"" . $match . "\">";
        }
    echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
    foreach($different as $diff)
        {
        if(!in_array($diff, $reject))
        {
        echo "<img src=\"" . $diff . "\">";
        }
        }
    echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
    foreach($retired_different as $unabletoget)
        {
        echo "<img src=\"" . $unabletoget . "\">";
        }
    echo "</textarea>";
STOPTHISCRAZYTHING;

6 个答案:

答案 0 :(得分:1)

您无法在heredoc语法中嵌入循环结构。你需要在:

之外处理你的foreach循环
$all = STOPTHISCRAZYTHING
    ...
STOPTHISCRAZYTHING;

修改

同样关于'echo'语句。您可以在heredoc赋值中使用变量,但您可以将其视为整个块是字符串赋值的右侧。不是执行PHP命令的块。

编辑2:

这是一个有效的例子,使用你的例子(部分)

    $all = <<< STOPTHISCRAZYTHING
        <br><br><textarea rows="30" cols ="100">
        <div align="center"><font size="7">I Have</font></div>
STOPTHISCRAZYTHING;

...注意你如何输入你需要的文字,你不必回声,也不必逃避引号。

docs:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

干杯

答案 1 :(得分:1)

我不确定你对It's all messed up, though it's printed.的意思,但在我看来它打印得很好但浏览器显示的是渲染版本:只需检查源代码。

如果您想将其视为真实文本,则需要执行的操作是:

  • 将其包装在<pre>标记中以保留换行符(或在回显之前使用字符串上的nl2br()
  • 在回显之前对变量使用htmlspecialchars(),以便将<等符号转换为html实体。

答案 2 :(得分:1)

您正在寻找output buffering

ob_start(); // Start capturing the script's output

echo "<br><br><textarea rows=\"30\" cols = \"100\">";
echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
foreach($same as $match)
    {
    echo "<img src=\"" . $match . "\">";
    }
echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
foreach($different as $diff)
    {
    if(!in_array($diff, $reject))
    {
    echo "<img src=\"" . $diff . "\">";
    }
    }
echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
foreach($retired_different as $unabletoget)
    {
    echo "<img src=\"" . $unabletoget . "\">";
    }
echo "</textarea>";

$all = ob_get_flush(); // Stop capturing output, and store the output 
// that was captured up until now into the variable $all

答案 3 :(得分:0)

我相信你要做的事情,你可以通过使用ob_start()和朋友来完成。 heredoc(&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;你不能把代码和所有东西放在一个heredoc字符串中。

http://php.net/manual/en/function.ob-start.php

ob_start()可以让你缓冲你打印的所有内容。然后,您可以使用其他ob函数来获取缓冲输出,并将其放入变量或任何您想要用它做的事情。

答案 4 :(得分:0)

你有一些有趣的包围......

您需要使用Heredoc吗? 为什么不使用“。”将它全部添加到变量中。运算符连接它们。另外,为简化起见,您可以混合使用单引号和双引号:

$all = "<br><br><textarea rows='30' cols = '100'>";
$all = $all . "<div align='center'><font size='7'>I Have</font></div>";
foreach($same as $match)
{
    $all = $all . "<img src='" . $match . "'>";
}
$all = $all . "<div align='center'><font size='7'>I Need</font></div>";
foreach($different as $diff)
{
    if(!in_array($diff, $reject))
{
$all = $all . "<img src='" . $diff . "'>";
$all = $all . "<div align='center'><font size='7'>I Am Unable To Obtain</font></div>";
foreach($retired_different as $unabletoget)
{
    $all = $all . "<img src='" . $unabletoget . "'>";
}
$all = $all . "</textarea>";

现在,当您回显$ all变量时,您将获得所需的完整输出

echo $all;

答案 5 :(得分:-1)

解决方案A:

<?php
   $all = <<< STOPTHISCRAZYTHING
    echo "&lt;br&gt;&lt;br&gt;&lt;textarea rows=\"30\" cols = \"100\"&gt;";
    echo "&lt;div align=\"center\"&gt;&lt;font size=\"7\"&gt;I Have&lt;/font&gt;&lt;/div&gt;";
    foreach($same as $match)
        {
        echo "&lt;img src=\"" . $match . "\"&gt;";
        }
    echo "&lt;div align=\"center\"&gt;&lt;font size=\"7\"&gt;I Need&lt;/font&gt;&lt;/div&gt;";
    foreach($different as $diff)
        {
        if(!in_array($diff, $reject))
        {
        echo "&lt;img src=\"" . $diff . "\"&gt;";
        }
        }
    echo "&lt;div align=\"center\"&gt;&lt;font size=\"7\"&gt;I Am Unable To Obtain&lt;/font&gt;&lt;/div&gt;";
    foreach($retired_different as $unabletoget)
        {
        echo "&lt;img src=\"" . $unabletoget . "\"&gt;";
        }
    echo "&lt;/textarea&gt;";
STOPTHISCRAZYTHING;

echo $all;

?>

解决方案B (使用htmlentities

<?php
   $all = <<< STOPTHISCRAZYTHING
    echo "<br><br><textarea rows=\"30\" cols = \"100\">";
    echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
    foreach($same as $match)
        {
        echo "<img src=\"" . $match . "\">";
        }
    echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
    foreach($different as $diff)
        {
        if(!in_array($diff, $reject))
        {
        echo "<img src=\"" . $diff . "\">";
        }
        }
    echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
    foreach($retired_different as $unabletoget)
        {
        echo "<img src=\"" . $unabletoget . "\">";
        }
    echo "</textarea>";
STOPTHISCRAZYTHING;

echo htmlentities($all);

?>

解决方案C (将其打包在<pre> ... </pre>代码中)

<?php
   $all = <<< STOPTHISCRAZYTHING
    <pre>
    echo "<br><br><textarea rows=\"30\" cols = \"100\">";
    echo "<div align=\"center\"><font size=\"7\">I Have</font></div>";
    foreach($same as $match)
        {
        echo "<img src=\"" . $match . "\">";
        }
    echo "<div align=\"center\"><font size=\"7\">I Need</font></div>";
    foreach($different as $diff)
        {
        if(!in_array($diff, $reject))
        {
        echo "<img src=\"" . $diff . "\">";
        }
        }
    echo "<div align=\"center\"><font size=\"7\">I Am Unable To Obtain</font></div>";
    foreach($retired_different as $unabletoget)
        {
        echo "<img src=\"" . $unabletoget . "\">";
        }
    echo "</textarea></pre>";
STOPTHISCRAZYTHING;

echo $all;

?>