执行字符串中的PHP代码 [英] Execute PHP code in a string

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

问题描述

我的页面内容保存在数据库中,并希望执行字符串中的任何php代码.因此,如果我的字符串是:

I have my page contents saved in a database and would like to execute any php code in the string. So if my string was:

<h1>Welcome</h1><?php echo $motto?><br/>

我只想执行echo $motto.使用eval()将尝试执行<h1>Welcome</h1>.

I only want to execute echo $motto. Using eval() will try to execute <h1>Welcome</h1>.

有什么办法吗?

推荐答案

不用说,您应该尽快找到另一个解决方案.在此期间,您可以评估如下代码:

Needless to say you should find another solution ASAP. In the meantime you can eval the code like this:

$str = '<h1>Welcome</h1><?php echo $motto?><br/>'; // Your DB content

eval("?> $str <?php ");

演示: http://codepad.org/ao2PPHN7

我不能太强调:eval是危险的,应用程序代码不应存在于数据库中.尝试使用 Smarty Dwoo 或我最喜欢的: Twig .

I can't stress that enough: eval is dangerous, and application code shouldn't be in the database. Try a template parser like Smarty, Dwoo, or my favorite: Twig.

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

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