Jump to content

Archived

This topic is now archived and is closed to further replies.

heisje

Get variables from external text file

Recommended Posts

.


have been trying all sorts of ideas to solve this, but nothing has worked - so here I come:
for this simple script:

[code]<?

$var1 = "one";
$var2 = "two";
$var3 = "three";

$text = "$var1, $var3";
echo $text

?>[/code]

output is: one, three

what is wanted:
to get the part "$var1, $var3" from an external text file, and have the script output determined according to what is specified in the text file, in this case the text file contents would only be:
$var1, $var3

but could also be
$var3, $var2

and then the output would be: three, two



thanks in advance for any ideas.

heisje

.

Share this post


Link to post
Share on other sites
<?
$var1 = "one";
$var2 = "two";
$var3 = "three";

$text = $var1.",".$var3;

echo $text
?>

Share this post


Link to post
Share on other sites
.


many thanks, but I do not get the point:
where is the external text file with the variables?
and how are they inserted in the script?

maybe my question was not very clear:
there exists an external text file specifying which variables shall determine $text:
for example:

text.txt with contents
$var1, $var3

tell the script which variables to use for $text.

need to read the text file and insert these variables, and get the script to output the text:
one, three

this is the question.


thanks,
heisje


..


Share this post


Link to post
Share on other sites
.


I shall try to describe the request in greater detail:

Suppose that I have 10,000 php pages that already contain the script:

[code]<?

$var1 = "one";
$var2 = "two";
$var3 = "three";

$text = "$var1, $var3";
echo $text

?>[/code]

output is: one, three
for all 10,000 pages

Now, suppose that I want to have the ability to change the output of all those 10,000 pages all at the same time by setting the value of $text.

One time I may want $text = "$var1, $var3";
Another time I may want $text = "$var3, $var2";
etc.

So, if I have an external text.txt file which I can change to $var1, $var3 - or var3, $var2 - or anything I want, then all the 10,000 pages will print the same thing accordingly.

It would be fine if the following worked, but of course it does not:

[code]<?

$var1 = "one";
$var2 = "two";
$var3 = "three";

$text = file_get_contents('text.txt');
echo $text

?>[/code]

to insert ex. $var1, $var3 and specify
$text = "$var1, $var3";

but this just prints: $var1, $var3
and not :
one, three
as intended.

I hope this is a bit more clear.

thanks,
heisje


.

Share this post


Link to post
Share on other sites
You want use the [a href=\"http://www.php.net/include/\" target=\"_blank\"]include()[/a] statement.

The include() statement will take the contents of the file and process the lines as if they were part of the source.

Ken

Share this post


Link to post
Share on other sites
.

many thanks, ken

I tried

<?

$var1 = "one";
$var2 = "two";
$var3 = "three";

$text = include('text.txt');
echo $text

?>

with contents in text.txt
$var1, $var3

but the output is:
$var1, $var31

and not:
one, three

as expected.

certainly I am doing something wrong - because apparently you did not mean what I have done by suggesting the use of include()

clarification?

thanks,
heisje


.

Share this post


Link to post
Share on other sites
You want the file text.txt should contain:
[code]<?php
$var1 = "one";
$var2 = "two";
$var3 = "three";
?>[/code]
Your script would be:
[code]<?php
include('text.txt');
echo "$var1, $var3";
?>[/code]

Ken

Share this post


Link to post
Share on other sites
.

many thanks ken, I see what you mean


unfortunately this solution will not work because

the $var in my example
can be defined only within the script (and are actually different from page to page) -

while the text.txt file
is a "control" file only, for the 10,000 pages, intended to control how many times and which $var are included in $text.

for example:

$text = '$var1 $var2 $var1 $var3';

or

$text = '$var1 $var1 $var1';

or

$text = '$var3';

any ideas?

thanks,
heisje


.

Share this post


Link to post
Share on other sites
So you want the include file to tell you which variables to print in the scripts. Here is one way that works.

This is the contents of the include file named test_inc.php:
[code]<?php
    $print_these = array($var1,$var3,$var5);
?>[/code]

Here is the test script:
[code]<?php
$var1 = 'This is var 1';
$var2 = 'This is var 2';
$var3 = 'This is var 3';
$var5 = 'This is var 5';

include('test_inc.php');

$tmp = array();
foreach($print_these as $var)
    $tmp[] = $var;
    
echo implode(', ',$tmp);
?>[/code]

You put the variable names you want to print in the array and use the foreach statement to get the values to print.

When you change the variables in the array, the output will change. You have to include the file after the variables are defined or this method won't work.

Ken

Share this post


Link to post
Share on other sites
.


ken, [b]many thanks indeed[/b]
this is exactly what is needed.

a simple and nice solution !! - and wonder now why I did not come up with this myself after trying so long?
well . . . . .

I hope this will also be of help to somebody else who will need to do the same thing in the future.
(I searched extensively in past posts and could not find something similar before).

thanks again to [b]ken[/b],
and thanks also go to [b]jvrothjr[/b] and to [b]barry[/b] (barand)
who contributed alternative ideas.

all the best to this wonderful community,
dedicated to solving problems.


heisje



.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.