如何将PHP输出捕获到变量中? [英] How do I capture PHP output into a variable?
问题描述
我正在生成大量XML,当用户单击表单按钮时,这些XML将作为发布变量传递到API.我还希望能够向用户显示XML.
I'm generating a ton of XML that is to be passed to an API as a post variable when a user click on a form button. I also want to be able to show the user the XML before hand.
代码在结构上类似于以下内容:
The code is sorta like the following in structure:
<?php
$lots of = "php";
?>
<xml>
<morexml>
<?php
while(){
?>
<somegeneratedxml>
<?php } ?>
<lastofthexml>
<?php ?>
<html>
<pre>
The XML for the user to preview
</pre>
<form>
<input id="xml" value="theXMLagain" />
</form>
</html>
我的XML正在生成,其中包含一些while循环和其他内容.然后需要在两个位置(预览和表单值)中显示它.
My XML is being generated with a few while loops and stuff. It then needs to be shown in the two places (the preview and the form value).
我的问题是.如何在变量中捕获生成的XML或其他内容,所以我只需要生成一次,然后按照需要将其打印出来就可以在预览中生成它,然后在表单值中再次生成它?
My question is. How do I capture the generated XML in a variable or whatever so I only have to generate it once and then just print it out as apposed to generating it inside the preview and then again inside the form value?
推荐答案
<?php ob_start(); ?>
<xml/>
<?php $xml = ob_get_clean(); ?>
<input value="<?php echo $xml ?>" />͏͏͏͏͏͏
这篇关于如何将PHP输出捕获到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!