0

I want to import variables from text file to PHP variables: my txt file looks like this:

var1=80
var2=130
var3=250

and i want load them as variables in php like this:

<?php
$var1=80;
$var2=130;
$var3=250;
?>

I tried explode("=", $file); but that prints

Array ( [0] => var1 [1] => 80 var2 [2] => 130 var3 [3] => 250 )

EDIT: I want output like this:

Array ( [var1] => 80 [var2] => 130 [var3] => 250 )
2
  • You're not splitting the file into lines. That's what you want to do first. Commented Jan 2, 2013 at 19:42
  • 2
    Check out php.net/manual/en/function.parse-ini-file.php Commented Jan 2, 2013 at 19:44

2 Answers 2

7
$vars = parse_ini_file('filename');
extract($vars);

though I'd keep them in array, by omitting extract() part

Sign up to request clarification or add additional context in comments.

2 Comments

+ This is the way to do it.
<?php $vars = parse_ini_file('vars.txt'); extract($vars); print_r($vars); ?> nothing?
0

Use the built-in parse_ini_file function for loading variables from external files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.