programing

PHP 출력을 변수로 캡처하려면 어떻게합니까?

projobs 2021. 1. 17. 10:22
반응형

PHP 출력을 변수로 캡처하려면 어떻게합니까?


사용자가 양식 버튼을 클릭 할 때 게시물 변수로 API에 전달할 XML을 생성하고 있습니다. 또한 사용자에게 XML을 미리 보여줄 수 있기를 원합니다.

코드 구조는 다음과 같습니다.

<?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 루프 등으로 생성되고 있습니다. 그런 다음 두 위치 (미리보기 및 양식 값)에 표시되어야합니다.

제 질문입니다. 생성 된 XML을 변수 또는 기타로 캡처하려면 한 번만 생성 한 다음 미리보기 내에서 생성 한 다음 다시 양식 값 내에서 생성하기 위해 인쇄하면됩니다.


<?php ob_start(); ?>
<xml/>
<?php $xml = ob_get_clean(); ?>
<input value="<?php echo $xml ?>" />͏͏͏͏͏͏

처음에 이것을 넣으십시오.

ob_start ();

그리고 버퍼를 되 찾으려면 :

$ value = ob_get_contents ();
ob_end_clean ();

자세한 내용은 http://us2.php.net/manual/en/ref.outcontrol.php 및 개별 기능을 참조하십시오.


PHP 출력 버퍼링 을 원하는 것 같습니다.

ob_start(); 
// make your XML file

$out1 = ob_get_contents();
//$out1 now contains your XML

출력 버퍼링은 출력을 "플러시"할 때까지 출력 전송을 중지합니다. 자세한 내용은 설명서 를 참조하십시오.


이것을 시도해 볼 수 있습니다.

<?php
$string = <<<XMLDoc
<?xml version='1.0'?>
<doc>
  <title>XML Document</title>
  <lotsofxml/>
  <fruits>
XMLDoc;

$fruits = array('apple', 'banana', 'orange');

foreach($fruits as $fruit) {
  $string .= "\n    <fruit>".$fruit."</fruit>";
}

$string .= "\n  </fruits>
</doc>";
?>
<html>
<!-- Show XML as HTML with entities; saves having to view source -->
<pre><?=str_replace("<", "&lt;", str_replace(">", "&gt;", $string))?></pre>
<textarea rows="8" cols="50"><?=$string?></textarea>
</html>

참조 URL : https://stackoverflow.com/questions/171318/how-do-i-capture-php-output-into-a-variable

반응형