JavaScript Editor Javascript validator     Website design 

Team LiB
Previous Section Next Section

Reading a File into an Array

It is often useful to work with a file as an array of data in memory. Frequently you might find yourself doing some operation on each line of an array. PHP provides a couple of features that simplify this type of operation. The cartoonifier.php program demonstrates one way of manipulating an entire file without using a file pointer.

Introducing the cartoonifier.php Program

The cartoonifier.php program illustrated in Figure 6.7 is a truly serious and weighty use of advanced server technology.

Click To expand
Figure 6.7: The cartoonifier program shows what would happen if Shakespeare were a cartoon character.

This program loads the entire sonnet into an array, steps through each line of the poem, and converts it to a unique cartoon dialect by performing a search and replace operation.

$fileName = "sonnet76.txt";

$sonnet = file($fileName);
$output = "";

foreach ($sonnet as $currentLine){
  $currentLine = str_replace("r", "w", $currentLine);
  $currentLine = str_replace("l", "w", $currentLine);
  $output .= rtrim($currentLine) . "<br>\n";
} // end foreach
  $output .= "You wascally wabbit!<br>\n";

print $output;


Loading the File into an Array with file()

There are also some shortcut file handling tricks that do not require you to create a file pointer. You might recall the readFile() command from the first chapter of this book. That file simply reads a file and echoes it to the output. The file() command is similar, because it does not require a file pointer. It opens a file for input and returns an array, with each line of the file occupying one element of the array. This can be an extremely easy way to work with files, because you can then use a foreach loop to step through each line of the file and perform modifications on it.


Reading an file into an array is attractive because it's easy and because once the file is in memory, you can work with it very quickly. The problem comes when you are working with very large files. The computer's memory is finite, and large files can quickly cause you problems. For larger data files, you'll probably want to use a one-line at a time approach using the fgets() function inside a loop.

Using str_replace() to Modify File Contents

Inside the foreach loop, it's a simple matter to convert all occurrences of "r" and "l" to the letter "w" with the str_replace() function. The resulting string is then added to the $output variable which will ultimately be printed to the screen.

Start example

This particular application is silly and pointless, but the ability to replace all occurrences of one string with another in a text file is very useful in a variety of circumstances. For example, you could replace every occurrence of the left brace (<) character in an HTML document with the &lt; sequence. This would result in a source code listing that could be directly viewed on the browser. Also, you might use such technology for form letters, where you take information in a text template and replace it with values pulled from the user or from another file.

End example

Team LiB
Previous Section Next Section

JavaScript Editor Javascript validator     Website design