PHP yerel komut çıktısı almak nasıl?

1 Cevap

Örneğin, foo.php in:

<?php echo 'hello'; ?>

Ve bar.php de, ben (merhaba) foo.php çıkışını almak ve tarayıcı çıktısı önce bazı biçimlendirme yapmak istiyorum. Bunu yapmak için herhangi bir yolu var mı?

Web sunucusu PHP ve Python komut hem de çalıştırabilirsiniz Ya da daha da ileri, bir PHP komut dosyası bir Python komut dosyası çıktı alabilirsiniz?

Edit: PHP function file_get_contents() can do this only for remote scripts. If it is used on local scripts, it will return the contents of the whole script. In the example above, it returns rather than hello. And I don't want to use exec()/system() things and CGI.

1 Cevap

Sen ob_start(); ve ob_get_contents(); bir dizeye PHP çıktı içeriğini okumak gibi PHP'nin çıkış tamponları ve işlevlerini kullanabilirsiniz.

İşte php.net üzerinde örnek:

<?php

function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>

Ve başka:

<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>