Read text fille as PHP code and run code

Trying everything I can find, so far nothing works.

I am an old ASP programmer and ASP has a function called EVAL. I know PHP has one too but I can’t get it to work right.

I have a TEXT file which I want to open and read the contents line by line and run each line as PHP code (setting variables). I know this shluld be trivial, but I am at a loss.

The file looks like:

$A = “abc”;
$B = “928”;
etc.

After opening the file, readiing line by line, I want Variables $A and $B to have values which can be use in the rest of the script.

Does someone have sample code which will accomplish this?

THANKS!!!

Read up on these two commands:

http://php.net/manual/en/function.parse-ini-file.php
http://us1.php.net/manual/en/function.extract.php

Then combine them

extract(parse_ini_file(‘yourfilename’), EXTR_PREFIX_ALL, ‘phphelp’);

You’ll need to remove the “$” from the text file.

Found this and it works:

foreach(explode("\n",file_get_contents(‘text.txt’)) as $cmd)
{
eval($cmd);
}

THANKS!

Sponsor our Newsletter | Privacy Policy | Terms of Service