PHP Files Processing Variable-Length Text Fields - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Files Processing Variable-Length Text Fields - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Wednesday, July 10, 2019

PHP Files Processing Variable-Length Text Fields

PHP Files


Processing Variable-Length Text Fields

Problem

You want to read delimited text fields from a file. You might, for example, have a database program that prints records one per line, with tabs between each field in the record, and you want to parse this data into an array.

Solution

Use fgetcsv() to read in each line and then split the fields based on their delimiter:

       $delim = '|';

       $fh = fopen('books.txt','r') or die("can't open: $php_errormsg");
       while (! feof($fh)) {
              $fields = fgetcsv($fh, 1000, $delim);
              // ... do something with the data ...
              print_r($fields);
       }
       fclose($fh) or die("can't close: $php_errormsg");

Discussion

To parse the following data in books.txt:

       Elmer Gantry|Sinclair Lewis|1927
       The Scarlatti Inheritance|Robert Ludlum|1971
       The Parsifal Mosaic|Robert Ludlum|1982
       Sophie's Choice|William Styron|1979

process each record as shown:

       $fh = fopen('books.txt','r') or die("can't open: $php_errormsg");
       while (! feof($fh)) {
              list($title,$author,$publication_year) = fgetcsv($fh, 1000, '|');
              // ... do something with the data ...
       }
       fclose($fh) or die("can't close: $php_errormsg");

The fgetcsv() function reads in lines from a file, like fgets(), but it also parses them into individual fields. As the name suggests, its default delimiter is a comma, but the function accepts any field. In this example, it’s a pipe.

The line-length argument to fgetcsv() needs to be at least as long as the longest record, so that a record doesn’t get truncated.

If any of your records contain your delimiter, fgetcsv() can parse these properly if the data is enclosed or escaped. By default, this is the double quote and backslash characters, respectively. You can change this:

       $fh = fopen('books.txt','r') or die("can't open: $php_errormsg");
       while (! feof($fh)) {
              list($title,$author,$publication_year) = fgetcsv($fh, 1000, '|', "'", '*');
              // ... do something with the data ...
       }
       fclose($fh) or die("can't close: $php_errormsg");

Here, records are wrapped in single quotes and the single-quote character is escaped using an asterisk.

If lines are not properly detected, enable the auto_detect_line_endings configuration option prior to opening the file:

       ini_set('auto_detect_line_endings', true);

       $fh = fopen('books.txt','r') or die("can't open: $php_errormsg");
       // rest of processing

No comments:

Post a Comment

Post Top Ad