How to execute text as PHP

I have a problem, I want to take the text and execute the text as PHP, but how to do it? 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 to do it? Please, help!

+5
source share
3 answers
+4
source

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

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 );
0

You can run both text and eval from the same script, but as previously mentioned. Security should be very tight. However, the eval function is really effective if you use it correctly. 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";
0
source

All Articles