csv", "r");
// Break each line of the file into three parts
while (list($name, $email, $phone) = fgetcsv($fh, 1024, ",")) {
// Output the data in HTML format
printf("
%s (%s) Tel. %s
", $name, $email, $phone);
}
?>
Note that you don??™t have to use fgetcsv() to parse such files; the file() and list()
functions accomplish the job quite nicely. Reconsider the preceding example:
// Read the file into an array
$users = file("/home/www/data/subscribers.csv");
foreach ($users as $user) {
// Break each line of the file into three parts
list($name, $email, $phone) = explode(",", $user);
// Output the data in HTML format
printf("
%s (%s) Tel. %s
", $name, $email, $phone);
}
?>
Reading a Specific Number of Characters
The fgets() function returns a certain number of characters read in through the
opened resource handle, or everything it has read up to the point when a newline or
an EOF character is encountered. Its prototype follows:
string fgets(resource handle [, int length])
294 CHAPTER 10 ?– WORKING WITH THE FI LE AND OPERATING SYSTEM
If the optional length parameter is omitted, 1,024 characters is assumed. In most
situations, this means that fgets() will encounter a newline character before reading
1,024 characters, thereby returning the next line with each successive call. An example
follows:
// Open a handle to users.txt
$fh = fopen("/home/www/data/users.
Pages:
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368