简体   繁体   English

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

[英]Store html content generated with PHP into a variable

I have some PHP code mixed with HTML and i need to store this into a variable but i can't figure out how can i do that. 我有一些PHP代码和HTML混合在一起,我需要将其存储到变量中,但是我不知道该怎么做。 When this code is executed, is called from a shortcode that calls the function and works perfectly.The PHP code render the HTML and shows the information from the DB, but now i need to store the result (this html divs boxes with information from the database) into a variable in order to send this variable to a function in wordpress that can update a page. 当执行此代码时,从调用该函数并能正常工作的短代码中调用该代码。PHP代码呈现HTML并显示来自DB的信息,但是现在我需要存储结果(this html divs boxes with information from the database)转换为变量,以便将此变量发送到可以更新页面的wordpress中的函数。 When i try it, there is a error that say echo isnt spected. 当我尝试时,出现错误,提示未指定echo。 I know that this variable doesn't spect php code inside, so how can i do that? 我知道这个变量不会在里面查看php代码,所以我该怎么做? store the result into a variable? 将结果存储到变量中?

any suggestion will be very apreciated thank you in advance: 任何建议都会非常感谢您:

$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>';

"; “;

Update 更新资料

Your code may benefit from the answer below but you're doing a different, and more dangerous, thing. 您的代码可能会从下面的答案中受益,但您正在做另一件事,并且更加危险。 You've a blob of HTML and PHP and want to execute it to store its results. 您拥有一堆HTML和PHP,并希望执行它来存储其结果。 To do this, you need something like eval() . 为此,您需要类似eval()东西。 Or equivalently, you can write the code to a temporary PHP file with unguessable name, then re-include it in your script while wrapping it in output buffering. 或等效地,您可以将代码写入具有无法猜测名称的临时PHP文件,然后将其重新包含在脚本中,同时将其包装在输出缓冲中。

I've seen it done using a "sandbox" server with limited rights, but that's complicated to implement. 我已经看到使用具有有限权限的“沙盒”服务器来完成此操作,但是实现起来很复杂。

The danger depends on where your code comes from. 危险取决于代码的来源。 If someone else can modify it, then he will be able to execute arbitrary code on your server, which you definitely want to avoid. 如果其他人可以修改它,那么他将能够在您的服务器上执行任意代码,您肯定要避免这样做。

For this reason some templating languages have been developed - you may want to look into Twig , for example. 因此,已经开发了一些模板语言 -例如,您可能需要研究Twig

Otherwise, you can now do: 否则,您现在可以执行以下操作:

$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();

Back to "storing mixed output into variables" 返回“将混合输出存储到变量中”

The correct way : 正确的方法

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

This can become awkward if you have functions that produce output. 如果您具有产生输出的函数,这可能会变得很尴尬。 You'll have to rewrite them to return the output (just like above, end with 您必须重写它们以返回输出(就像上面一样,结尾为

 return $html;

and then wherever you had 然后无论你在哪里

 callToFunction(parameters);

you'll refactor with 您将重构

 echo callToFunction(parameters);

Then, further refactor it to read 然后,进一步将其重构为读取

 $html .= callToFunction(parameters)

iteratively. 反复地。

The lazy way 懒惰的方式

Using output buffering . 使用输出缓冲 A great way to hog memory, lose control of your script, and play merry hell whenever you'll want to debug it (unless you go with Xdebug, but then why do the lazy thing in the first place?). 节省内存,失去对脚本的控制以及在任何需要调试的时候玩地狱的好方法(除非您使用Xdebug,但是为什么首先要做些懒惰的事情?)。

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

What you're looking for is output buffering. 您正在寻找的是输出缓冲。

Call ob_start(); 调用ob_start(); before echo'ing your content, then at the end: 在回显您的内容之前,然后最后:

$content = ob_get_clean();

Everything that was output between those two functions will then be stored in $content . 这两个函数之间输出的所有内容都将存储在$content

There's further information on these functions - and other options as well - in the PHP manual . PHP手册中,有关于这些功能的更多信息以及其他选项。

Keep in mind output buffering should generally be a last resort - don't get too comfortable with doing this everywhere! 请记住,输出缓冲通常应该是不得已的方法-不要在任何地方都习惯这样做!

By the way, you can avoid all the calls to echo by just ending the PHP block ( ?> and starting it again when you're done ( <?php ). 顺便说一句,您可以通过结束PHP块( ?>并在完成后重新启动( <?php )来避免所有echo

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

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