0

I have a text file with the following data

//info.txt
$ip = "192.168.12.201"
$sno = "some text"
$mac = "some text"

I want to be able to convert these variables from the text file into PHP variables to be checked with presaved data. I'm able to do this in python but it causes and extra redundant step. Could what I'm trying to achieve be done in PHP?

KelvTheKid
  • 15
  • 1
  • 8
  • If you're able to put inside the text file, you should be able to include it using include('info.txt'); - This may also help http://php.net/manual/en/function.fopen.php – Matt Jun 22 '17 at 06:54
  • Possible duplicate of [How to evaluate formula passed as string in PHP?](https://stackoverflow.com/questions/1015242/how-to-evaluate-formula-passed-as-string-in-php) – Alexander Jun 22 '17 at 08:29

3 Answers3

0

For this approach, you need to read the file first, then use eval() to execute the command inside the txt file.

<?php   
$text = file_get_contents("info.txt");
eval($text);
// display variable
var_dump($ip);
var_dump($sno);
var_dump($mac);

But, I'm not sure this approach is secure

Dolly Aswin
  • 2,684
  • 1
  • 20
  • 23
0

Variable variable is not recommended, but you can do this: readline to get value like: $value1 = 'ip': $value2 = '192.168.12.201' so:

$$value1 = $value2;

thats mean

$ip = '192.168.12.201';
0
$var = 123;

$val = "testo";

${"_testoacaso_".$var} = $val;

$_testoacaso_123 = $val;
Ryosaku
  • 453
  • 4
  • 14