Prev | Current Page 355 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

The optional delimiter
parameter (by default set to a comma) identifies the character used to delimit each
field. The optional enclosure parameter (by default set to a double quote) identifies a
character used to enclose field values, which is useful when the assigned delimiter value
might also appear within the field value, albeit under a different context.
?– Note Comma-separated value (CSV) files are commonly used when importing files between applications.
Microsoft Excel and Access, MySQL, Oracle, and PostgreSQL are just a few of the applications and databases
capable of both importing and exporting CSV data. Additionally, languages such as Perl, Python,
and PHP are particularly efficient at parsing delimited data.
Consider a scenario in which weekly newsletter subscriber data is cached to a file
for perusal by the marketing staff. This file might look like this:
Jason Gilmore,jason@example.com,614-555-1234
Bob Newhart,bob@example.com,510-555-9999
Carlene Ribhurt,carlene@example.com,216-555-0987
Always eager to barrage the IT department with dubious requests, the marketing
staff asks that the information also be made available for viewing on the Web. Thankfully,
this is easily accomplished with fgetcsv(). The following example parses the file:
CHAPTER 10 ?–  WORKING WITH T HE FILE A ND OPERATING SYSTEM 293
// Open the subscribers data file
$fh = fopen("/home/www/data/subscribers.


Pages:
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367