如何将 PHP 输出捕获到变量中? [英] How do I capture PHP output into a variable?

查看:38
本文介绍了如何将 PHP 输出捕获到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成大量 XML,当用户单击表单按钮时,这些 XML 将作为 post 变量传递给 API.我还希望能够事先向用户展示 XML.

代码结构如下:

<xml><morexml><?php尽管(){?><一些生成的xml><?php } ?><lastofthexml><?php ?><预>供用户预览的 XML

<表格><input id="xml" value="theXMLagain"/></表单>

我的 XML 是通过一些 while 循环和其他东西生成的.然后它需要显示在两个地方(预览和表单值).

我的问题是.如何在变量或其他任何内容中捕获生成的 XML,以便我只需要生成一次,然后将其打印出来,以便在预览中生成它,然后在表单值中再次生成?

解决方案

<xml/><?php $xml = ob_get_clean();?><input value="<?php echo $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>

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).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆