Re: how to read a file into a variable?

From: J.O. Aho (user_at_example.net)
Date: 11/03/04


Date: Wed, 03 Nov 2004 22:48:07 +0100

shawn wrote:
> One more thing:
>
> include will include the file and immediately displays it in the page; I
> want to be able to hold that content and use it elsewhere, such as send it
> out in email.

Everything depends on how you make the include file to look like.

--include file1--
<html><head><title>something</title></head><body>something else</body></html>
--- eof ---

--include file2--
<?PHP
$variable="<html><head><title>something</title></head><body>something
else</body></html>";
?>
--- eof ---

Including the first file would result into that you get the content pasted to
the html page, while the second file would give you a variable which you can
affect as you wish. Keep in mind with the second one, you _CAN'T_ do a

echo $variable;

as that would result in pure HTML code, you would need to do something like

echo eregi_replace(">","&gt;"eregi_replace("<","&lt;",$variable));

This way you will get the output you wanted (there are most likely better ways
to make the replacement of characters).

If you still want to use the first form of include file, then use the function
  file():

$array_of_the_whole_file=file('file1.php');

Now you can easilly parse or what ever you want with the file, you can
implode() it too and you get a single variable with the whole content. Check
your php.ini to see how much memory you can use for php, as this will limit
the size of the file you want to read.

  //Aho



Relevant Pages

  • Re: Interesting data storage problem
    ... > write out to files a fair amount of data, on the rough order of 10 MB. ... number is the block's location in the second file. ... specifically recorded in the first file. ... the record data along with the extra data position and size. ...
    (comp.lang.java.programmer)
  • Re: Working with two input files
    ... First file: data1 data2 data3 ... Second file: 00:01 begin block 00:02 text data1 00:03 end block 01:10 begin block 01:11 text data2 01:12 end block 01:30 begin block 01:31 text data1 01:32 other data 01:33 end block 01:51 begin block 02:12 text data4 02:13 end block 02:11 begin block 02:12 text data3 ... from first file and don't output blocks with data, ... load it string by string and run script with different values of data, ...
    (comp.lang.awk)
  • Re: array values not visible outside while loop? scope issue?
    ... Can you please explain your script a bit so that I can ... The program will read the first file and then the second file ... awk is really great for processing files. ...
    (comp.lang.awk)
  • Re: Concatenating files
    ... involve internal data which has to be matched up in various esoteric ... constant-time operation on the first file (such as updating some ... header) and an Ooperation on the second file. ... Or maybe the running time of the join operation depends on the ...
    (comp.programming)
  • Re: Image issues
    ... session variable in the first file and try to retrieve it in the second ... Since you are effectively calling a PHP ... // Check the querystring ... So do the spirits who are ...
    (alt.php)