Online Shopping : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

59 of 66 people (89%) answered Yes
Recently 8 of 10 people (80%) answered Yes


How do I include pre-formatted text into a PHP page?

Jan 2nd, 2009 00:25
Venus venui, healthy man, sheana krat, dman, Vimal Pratap, raun siegal, Articles Hosting, Philip Olson, Johan Naudé,

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 
here (many examples exist):
  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 
      include 'some/file.txt'; 
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 
and Curl.  
An example using file():
    $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.
Another example.  Let's execute a Linux fortune and ouput the results 
an HTML page:
    $fortune = `fortune`;
    print nl2br($fortune);
  fyi: We used ``backticks`` to execute the fortune command on 
       the system.  Read about executing commands in php here:
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:
see this article :