View Full Version : Loading variables from a text file
12-01-2010, 03:30 AM
I am trying to load a series of variables from a text document for use within a web page. The text document shows them as a list, ie.
$variable_a=yes it works;
From here I want to call the variables inside the main webpage. So I have tried pulling the variables from the text document using include but tht displays them at the top of the page in a single line. All I want is for them to be included in the php so that further down when I call the variable it displays "yes it works" as the result! I must usea txt file as it has a backend being edited by novices who I dont want to change this main page!
Any help is much appreciated!!
12-01-2010, 04:14 AM
Your pages must be .php (instead of .html), unless you specify that in an .htaccess file.
Below are some basic methods ... different ways to include text and PHP scripts ...
Copy and paste this into notepad, save it as "test.php" and upload it to your website.
See what happens ...
$variable_a="yes it works";
// or include a PHP script
// now, the normal HTML stuff
blah blah blah<br>
Does it work? <?=$variable_a?> <br>
This is more close to what you are attempting ... to include an external .txt file into your web page ...
Save some text in a file called "mytext.txt" ....
Now is the time for all good men to come to the aid of their country.
Then try this script ... call it "test2.php" ...
blah blah blah<br>
Maybe you want to include some random lines of text from a remote text file.
(It's a random quote from my website)
12-01-2010, 04:52 AM
Take a look at this: http://us2.php.net/manual/en/function.file.php
It converts a plain text listing into an array. You may have to use trim() to remove the end of line characters and any end whitespace. It's about the best bet for using .txt the way you want.
12-01-2010, 10:24 AM
If it has to be a text file for security, how about parse_ini_file()?
Instead of having the variables specified as:
$variable_a = "blah";
12-01-2010, 12:02 PM
the extension is irrelevant unless you call as an absolute url ... e.g
echo $var1; // will work
// wont work
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.