Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute text as PHP

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!

like image 501
S17514 Avatar asked May 20 '12 00:05

S17514


People also ask

How is a PHP script executed?

Example #1 Execute PHP script as shell scriptThe PHP executable can be used to run PHP scripts absolutely independent of the web server. On Unix systems, the special #! (or "shebang") first line should be added to PHP scripts so that the system can automatically tell which program should run the script.

Can I run PHP script from command line?

You can run PHP scripts in the Command Line for a particular PHP version used in Plesk.


2 Answers

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

like image 65
DanC Avatar answered Oct 12 '22 00:10

DanC


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";
like image 20
Kevin Ng Avatar answered Oct 12 '22 00:10

Kevin Ng