用于执行PHP代码的字符串变量 [英] String variable to execute PHP code

查看:94
本文介绍了用于执行PHP代码的字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是从数据库中提取html和PHP代码,然后执行它.例如,我可能有:

What I want to do is pull html and PHP code out from a database and then execute it. So for example I may have:

<?php 
  $test = <<<END
    <p> <?php 
    echo time(); 
    ?> </p>
    END;
  echo $test;
?>

我想要的是让$ test打印

What I want is to get $test to print

<p> 12:00PM </p>                  //right

代替打印:

<p> <?php echo time(); ?> </p>    //wrong

当我使用echo函数时发生的情况.

as occurs when I use the echo function.

请不要告诉我如何使用JavaScript或其他解决方法来做同样的事情.相反,请坚持该问题,并记住该示例只是演示我的问题的示例.实际的代码要复杂得多.

Please do not tell me how to do the same thing with JavaScript or other work around. Instead stick to the question and remember the example is just an example to demonstrate my problem. The actual code is much more complicated.

我查看了包含PHP代码的Javascript字符串变量但没有答案.

谢谢

布雷特(Brett)

推荐答案

您可以使用 eval ()为此

$test = <<<END
<p> <?php 
echo time(); 
?> </p>
END;


ob_start();
eval("?>$test");
$result = ob_get_clean();

这篇关于用于执行PHP代码的字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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