将字符串作为 PHP 代码执行并从代码返回打印文本的函数
我正在尝试创建一个函数来解析 php 代码并以纯文本形式返回结果,就像在浏览器中读取结果一样。像这样:
public function PHPToText($data, $php_text) {
//TODO code
return $text;
}
我会像这样调用该函数,其参数如下所示:
$data = array('email' => '[email protected]');
$string = "<?= " . '$data' . "['email']" . "?>";
$text = $this->PHPToText($data, $string);
现在 echo $text
应该给出:[email protected]
任何想法或功能能很好地实现这一点吗?
I am trying to create a function that would parse php code and return the result in pure text, as if it was being read in a browser. Like this one:
public function PHPToText($data, $php_text) {
//TODO code
return $text;
}
I would call the function like this, with the params that you see below:
$data = array('email' => '[email protected]');
$string = "<?= " . '$data' . "['email']" . "?>";
$text = $this->PHPToText($data, $string);
Now echo $text
should give: [email protected]
Any ideas or a function that can achieve this nicely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个糟糕的糟糕的糟糕的想法,但基本上:
你真的应该重新考虑这种设计。执行动态生成的代码本质上从来都不是一个好主意。
It's a bad bad bad bad bad idea, but basically:
You really should reconsider this sort of design. Executing dynamically generated code is essentially NEVER a good idea.
在这种情况下,应该使用 eval() 来完成
,但永远记住:eval 是邪恶的!
in this case it should be done with eval()
But always remember: eval is evil!
您将需要使用
eval()
函数 http://www.php.net/ eval 以便解析变量$string
内的标签You will need to use the
eval()
function http://www.php.net/eval in order to parse the tags inside your variable$string