PDA

View Full Version : file contents into an array


MysticFallout
10-18-2002, 08:14 PM
i have an external script called vars.php which holds variables like

$bgcolor = '#000000';
$header_text = "Welcome Home";

and so on. what i want to do is pull each line into an array to put the value of each variable into a txt box for user editing. i know how to rewrite this info to the file, but i still havent been able to pull the values into an array. i have tried:

Code:

$vars = implode(" ", file("vars.php"));

echo "$vars[0]<br />$vars[1]";



but that pulls <?php in as the vars. can anyone point me at least in the right direction? id like to use MySQL for this, but i havent got it enabled at my host yet. plus my friend who will use this script also doesnt have MySQL accessibility at all :(

mordred
10-18-2002, 09:02 PM
I'm not quite sure about your problem. If you got <?php echoed, then that means that you were succesful at transforming the file contens into an array, and that the very first line in the file vars.php is in fact <?php (which makes kind of sense, since it appears as if this file should be used for configuration or sth similar). If I'm right on this, your real problem is that you want to remove the first element of this array (the <?php) and possibly the last element (the corresponding ?>).

Luckily, PHP supplies us with a wide and confusing range of array functions. The is more than one way to solve your problem, this is just the first that was on my mind:


$arr = file("vars.php");
array_shift($arr); // remove the first element
array_pop($arr); // remove the last element

$str = implode(" ", $arr);
echo $str; // voila