将 PHP 生成的 html 内容存储到变量中

Store html content generated with PHP into a variable

我有一些 PHP 代码与 HTML 混合在一起,我需要将其存储到一个变量中,但我不知道该怎么做。执行此代码时,从调用函数并工作的短代码调用 perfectly.The PHP 代码呈现 HTML 并显示来自数据库的信息,但现在我需要存储结果 (this html divs boxes with information from the database) 到一个变量中,以便将此变量发送到 wordpress 中可以更新页面的函数。当我尝试它时,出现一个错误,提示未检测到回声。我知道这个变量不检查 php 里面的代码,那我该怎么做呢?将结果存储到变量中?

任何建议将不胜感激 提前致谢:

$ultimosProyectosHome+="
echo '<div class="cont-proyectos">';
echo '<div style="text-align:center;"><a href="'. $url_actual .'"?      page_id="'. $id_paginaFicha .'" target="_blank">';if($pregunta6Valor!=""){echo '<img src="http://www.xxx.xx/xxx/xxx/'.$pregunta6Valor.'" alt="Cover"></a></div>';} else{echo '<img src="'.$imgDesafio.'" alt="Cover"></a></div>';}
                            echo '<h3>';if($pregunta2Valor != ""){ echo $pregunta2Valor;}else{echo $Nodefinido;}echo'</h3>';

                             echo'<a href="'. $url_actual .'"?page_id="'. $id_paginaFicha .'" target="_blank">';
                                    echo'<h4>';if($pregunta1Valor != ""){ echo $pregunta1Valor;}else{echo $Nodefinido;} echo'</h4>';
                                echo'</a>';                                    
                                echo'<p>'; if($pregunta3Valor != ""){ echo getSubString($pregunta3Valor,198,$id_paginaFicha);}else{echo $Nodefinido;} echo'</p>'; 

                                echo'<div class="redesSocialesTLP" id="redes">';
                                    echo'<a href="https://twitter.com/intent/tweet?url="'. $url_client .'"?page_id="'. $id_paginaFicha .'"&text="'. $pregunta1Valor .'" target="_blank">';

                                        echo'<i class="fa fa-twitter fa-2x"></i>';
                                    echo'</a>';


                                    echo'<a href="https://www.facebook.com/sharer/sharer.php?u="'. $url_client .'"?page_id="'. $id_paginaFicha .'" target="_blank">';

                                        echo'<i class="fa fa-facebook fa-2x"></i>';
                                    echo'</a>';

                                    echo'<a href="https://plus.google.com/share?url="'. $url_client .'"?page_id="'. $id_paginaFicha .'" target="_blank">';

                                        echo'<i class="fa fa-google-plus fa-2x"></i>';
                                    echo'</a>';
                                echo'</div>';
                            echo'</div>';

";

您正在寻找的是输出缓冲。

在回显您的内容之前调用 ob_start();,然后在最后:

$content = ob_get_clean();

这两个函数之间输出的所有内容都将存储在 $content

PHP manual.

中有关于这些功能以及其他选项的更多信息

请记住,输出缓冲通常应该是最后的手段 - 不要在任何地方都这样做太舒服了!

顺便说一下,您可以通过结束 PHP 块(?> 并在完成后再次启动它来避免对 echo 的所有调用(<?php).

更新

您的代码可能会从下面的答案中获益,但您正在做一件不同的、更危险的事情。您有一个 HTML 和 PHP 的 blob 并且想要 执行 它来存储它的结果。为此,您需要 eval() 之类的东西。或者等效地,您可以将代码写入一个临时 PHP 文件,其名称不可猜测,然后将其重新包含在您的脚本中,同时将其包装在输出缓冲中。

我看到它是使用权限有限的 "sandbox" 服务器完成的,但是实现起来很复杂。

危险取决于您的代码来自何处。如果其他人可以修改它,那么他就可以在您的服务器上执行任意代码,这是您绝对要避免的。

出于这个原因,已经开发了一些 模板语言 - 你可能 例如,想查看 Twig

否则,您现在可以这样做:

$bunch = "<BUNCH OF MIXED CODE FROM ABOVE>";

ob_start();
try {
     eval($bunch);
} catch (\Exception $err) {
     print "There has been an error.";
}
$ultimosProyectosHome += ob_get_clean();

返回"storing mixed output into variables"

正确的方法

 $html = '';
 ...
 // Remove all occurrences of this
 echo "Something";
 // and this
 echo "<br />";
 // and write it like this instead.
 $html .= 'Something';
 $html .= '<br />';

如果您有生成输出的函数,这会变得很尴尬。您必须将它们重写为 return 输出(就像上面一样,以

结尾
 return $html;

然后无论你在哪里

 callToFunction(parameters);

您将使用

进行重构
 echo callToFunction(parameters);

然后,进一步重构为

 $html .= callToFunction(parameters)

迭代。

偷懒的方式

使用 output buffering。一个很好的方式来占用内存,失去对你的脚本的控制,并且在你想要调试它的时候玩得开心(除非你使用 Xdebug,但是那么为什么首先要做懒惰的事情?)。

 ob_start();
 ...
 $html = ob_get_clean();