Entry
How do I include pre-formatted text into a PHP page?
Jul 1st, 2008 11:23
sheana krat, dman, Vimal Pratap, raun siegal, Articles Hosting, Philip Olson, Johan Naudé, http://www.ttnr.org
There are many ways to insert a text file into PHP. It's common to use
include() for this purpose. include can be read about in the PHP
manual
here (many examples exist):
http://www.php.net/include
include 'filename'; : Includes and evaluates the
specified file.
The file is evaluated, so PHP code is evaluated as PHP code. PHP code
is written within valid PHP tags, most commonly <?php ?>.
In the example below, we'll preserve whitespace in HTML by using
<pre>.
<pre>
<?php
include 'some/file.txt';
?>
</pre>
Or, to open the file up into a variable, consider file(). file opens a
file as an array, each element being a line in that array. file() is
sexy in that it opens files in one line of code :) Also see
fopen/fgets
and Curl.
An example using file():
<?php
$filename = 'afile.txt';
$lines = file($filename);
// $lines is an array. let's print the third line of afile.txt
print $lines[2];
// open file as an array, implode that array into a string
$file_string = implode('', file($filename));
// print string, first converting newlines into <br \>'s
print nl2br($file_string);
?>
Read the PHP Manual for information regarding these functions.
http://www.php.net/manual/
Another example. Let's execute a Linux fortune and ouput the results
to
an HTML page:
<?php
$fortune = `fortune`;
print nl2br($fortune);
?>
fyi: We used ``backticks`` to execute the fortune command on
the system. Read about executing commands in php here:
http://www.php.net/exec
http://www.php.net/manual/en/language.operators.execution.php
Also keep in mind that different operating systems have different
"versions" of "newlines":
\n = unix
\r\n = windows
\r = mac
More information on using strings in PHP:
http://www.php.net/manual/en/language.types.string.php
====
see this article :
softpress.com/kb/article.php?id=715
http://www.indianhandicraftsexporters.com
http://www.topgiftsideas.com
http://www.eindianweddingcards.com