Jump to content


Photo

Get variables from external text file


  • Please log in to reply
9 replies to this topic

#1 heisje

heisje
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFriesland, Netherlands - Athens, Greece

Posted 22 June 2006 - 07:05 PM

.


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

<?

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

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

?>

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

.

#2 jvrothjr

jvrothjr
  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts
  • LocationOhio

Posted 22 June 2006 - 07:11 PM

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

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

echo $text
?>

If you understand everything you know nothing!

http://rcchjr.awardspace.com/

#3 heisje

heisje
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFriesland, Netherlands - Athens, Greece

Posted 22 June 2006 - 07:29 PM

.


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


..




#4 heisje

heisje
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFriesland, Netherlands - Athens, Greece

Posted 22 June 2006 - 09:26 PM

.


I shall try to describe the request in greater detail:

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

<?

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

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

?>

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:

<?

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

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

?>

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


.

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 June 2006 - 10:30 PM

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

#6 heisje

heisje
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFriesland, Netherlands - Athens, Greece

Posted 22 June 2006 - 11:47 PM

.

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


.

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 June 2006 - 11:59 PM

You want the file text.txt should contain:
<?php
$var1 = "one";
$var2 = "two";
$var3 = "three";
?>
Your script would be:
<?php
include('text.txt');
echo "$var1, $var3";
?>

Ken

#8 heisje

heisje
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFriesland, Netherlands - Athens, Greece

Posted 23 June 2006 - 12:26 AM

.

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


.

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 June 2006 - 02:39 AM

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:
<?php
    $print_these = array($var1,$var3,$var5);
?>

Here is the test script:
<?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);
?>

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

#10 heisje

heisje
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFriesland, Netherlands - Athens, Greece

Posted 23 June 2006 - 06:22 AM

.


ken, many thanks indeed
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 ken,
and thanks also go to jvrothjr and to barry (barand)
who contributed alternative ideas.

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


heisje



.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users