如何将包含内容作为字符串? [英] How to get include contents as a string?

查看:87
本文介绍了如何将包含内容作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

执行PHP文件,并将结果作为字符串返回

PHP捕获打印/需要变量输出

我试图将include的内容添加到字符串中。这可能吗?

I am trying to get the contents of an include to a string. Is that possible?

例如,如果我有一个test.php文件:

For example, if I have a test.php file:

<?php
echo 'a is equal to '.$a;
?>

我需要一个函数,比如说include_to_string包含test.php并返回输出的内容在字符串中。

I need a function, say include_to_string to include the test.php and return what would be output by in in a string.

类似于:

<?php
$a = 4;
$string = include_to_string(test.php); // $string = "a is equal to 4"
?>


推荐答案

<?php

  ob_start();
  include 'test.php';
  $string = ob_get_clean();

我认为你想要的。请参阅输出缓冲

I think is what you want. See output buffering.

这篇关于如何将包含内容作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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