将回波捕获到变量中 [英] capturing echo into a variable

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

问题描述

我正在使用动态函数名称(类似这样)来调用函数

I am calling functions using dynamic function names (something like this)

$unsafeFunctionName = $_POST['function_name'];
$safeFunctionName   = safe($unsafeFunctionName); // custom safe() function

然后,我想在函数的返回值周围包装一些xml(类似这样):

Then I am wanting to wrap some xml around the returned value of the function (something like this):

// xml header etc already created
$result = "<return_value>" . $safeFunctionName() . "</return_value>";

问题是,有时函数返回一个值,但是有时函数回显是一个值.我想做的是将回声捕获到一个变量中,但是,我编写的代码将需要以任何一种方式工作(意味着,如果函数返回一个值,或者回声是一个字符串).

Problem is, sometimes the function returns a value, but sometimes, the function echo's a value. What I want to do is capture that echo into a variable, but, the code I write would need to work either way (meaning, if function returns a value, or echo's a string).

不太确定从哪里开始〜有什么想法吗?

Not quite sure where to start ~ any ideas?

推荐答案

让我以这样的方式开头:

Let me preface this by saying:

在使用自定义函数调用业务时要格外小心.我假设您知道这可能有多危险,这就是您以某种方式进行清洁的原因.

Be careful with that custom function calling business. I am assuming you know how dangerous this can be which is why you're cleaning it somehow.

过去,您想要的就是输出缓冲:

function hello() {
    print "Hello World";
}
ob_start();
hello();
$output = ob_get_clean();
print "--" . $output . "--";

(我添加了破折号以表明它最初并未被打印)

(I added the dashes to show it's not being printed at first)

上面将输出--Hello World--

这篇关于将回波捕获到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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