简体   繁体   English

PHP 评估字符串 - 替代?

[英]PHP Eval String - Alternative?

In a text file (eg. file1.txt), let's say I have a line of php:在文本文件(例如 file1.txt)中,假设我有一行 php:

myArray['index'] = "awesome"

In my PHP file (eg. file2.php) I have this line of text imported and assigned to $string.在我的 PHP 文件(例如 file2.php)中,我将这行文本导入并分配给 $string。

Is there a way to evaluate the contents of $string without using the eval function?有没有办法在不使用 eval 函数的情况下评估 $string 的内容?

I currently do:我目前这样做:

eval("{$string};");

To clarify the reason I am asking: I've read that using eval is "evil".为了澄清我问的原因:我读过使用 eval 是“邪恶的”。 For this reason I want to see if there is an alternative method of evaluating php code that is stored in a text file.出于这个原因,我想看看是否有一种替代方法来评估存储在文本文件中的 php 代码。 I know it might seem weird.我知道这可能看起来很奇怪。 You may be thinking "why don't you just write the code in the php file?", but the reason I'm doing this is not really my question.您可能会想“为什么不直接在 php 文件中编写代码?”,但我这样做的原因并不是我真正的问题。 I'm just wondering about alternatives to using eval.我只是想知道使用 eval 的替代方法。 Thanks.谢谢。

Put a <?php in the top of the file and then include() it.将 <?php 放在文件顶部,然后 include() 它。 But if any of the file's contents come from untrusted users, you have a security vulnerability on your hands.但是,如果文件的任何内容来自不受信任的用户,那么您手上就有一个安全漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM