6

I have a problem, I want to grab text and execute text as PHP, but how do I do this? For example I have this code in a .txt file:

$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.")));

Now the problem is that I grabbed this text and I want to execute it as a PHP script, how do I do this? Please help!

S17514
  • 265
  • 2
  • 7
  • 16
  • 2
    I hope you're aware this is an invitation to get hacked if you're doing this with user input? – John Carter May 20 '12 at 00:32
  • 7
    If you're asking how to use `eval`, you should **definitely** not be using `eval`. –  May 20 '12 at 00:39

4 Answers4

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

BE CAREFUL!

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

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

DanC
  • 8,595
  • 9
  • 42
  • 64
  • 2
    Just a side note: `eval()` + `file_get_contents()` is essentially the same thing as an `include`. If you were to do that, you might as well add ` – Matthew May 20 '12 at 00:45
1

You can run both text and eval from the same script but like previously mentioned. Security must be really tight. Nevertheless, the eval funtion is really powerful if you use it properly. Try the code below.

$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";
Kevin Ng
  • 2,146
  • 1
  • 13
  • 18
0

You can evaluate text as PHP using eval; however, read below for a very important disclaimer!

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

// $result is a string containing PHP code. Be sure you trust the source of
 // the PHP code prior to running it!
eval( $result );
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
0

You can use the include() or the require() funcions :

include(/your_text_file.txt);

// OR
require(/your_text_file.txt);

Or the Eval function (like the 1st comment)

A.MARSHALL
  • 21
  • 3