6

我有一个问题,我想获取文本并将文本作为 PHP 执行,但是我该怎么做呢?例如,我在 .txt 文件中有此代码:

$tweetcpitems->post('statuses/update', array('status' => wordFilter("The item Blue has             been released on Club Penguin.")));
$tweetcpitems->post('statuses/update', array('status' => wordFilter("The item Green has been     released on Club Penguin.")));

现在的问题是我抓取了这个文本,我想将它作为 PHP 脚本执行,我该怎么做?请帮忙!

4

4 回答 4

5
eval(file_get_contents('yourfile.txt'));

当心!

http://php.net/manual/en/function.file-get-contents.php

http://php.net/manual/en/function.eval.php

于 2012-05-20T00:30:41.603 回答
1

您可以从同一个脚本同时运行 text 和 eval,但就像前面提到的那样。安全必须非常严格。不过,如果使用得当,eval 功能确实非常强大。试试下面的代码。

$b = 123;
$a = "hello <?php echo 'meeeee'; ?>. I just passed $b from the mother script. Now I will pass a value back to the mother script" . '<?php $c; $c = 1 + 8; ?>' .
     "I was call within a function, therefore my variable can't passed to the global script. Nonetheless, let try something globally" .
     "<?php 
        global \$d;
        \$d = 'I am now a global var. Take care though, don\\'t let anyone edit your file' ;
      ";

function parseTxtAsCode($invalue){
    if( !is_string($invalue) ) return false;
    eval('?>' . $invalue);
    echo "\n\n\n\n Can't believe I got this from my child: $c \n";
}

parseTxtAsCode($a);
echo "\n\n\n I am global and this is what I got from the eval function: $d";
于 2017-05-19T07:00:10.143 回答
0

您可以使用eval将文本评估为 PHP ;但是,请阅读下面的非常重要的免责声明!

eval() 语言结构非常危险,因为它允许执行任意 PHP 代码。因此不鼓励使用它。如果您已仔细验证除了使用此构造之外别无选择,请特别注意不要将任何用户提供的数据传入其中,而无需事先正确验证。

// $result is a string containing PHP code. Be sure you trust the source of
 // the PHP code prior to running it!
eval( $result );
于 2012-05-20T00:32:22.867 回答
0

您可以使用 include() 或 require() 函数:

include(/your_text_file.txt);

// OR
require(/your_text_file.txt);

或 Eval 函数(如第一条评论)

于 2020-04-15T18:58:11.140 回答