1.11.3. Discussion
Data in which each field is allotted a fixed number of characters per
line may look like this list of books, titles, and publication dates:
$booklist=<<<END
Elmer Gantry Sinclair Lewis1927
The Scarlatti InheritanceRobert Ludlum 1971
The Parsifal Mosaic Robert Ludlum 1982
Sophie's Choice William Styron1979
END;
In each line, the title occupies the first 25 characters, the
author's name the next 14 characters, and the
publication year the next 4 characters. Knowing those field widths,
it's straightforward to use substr(
) to parse the fields into an array:
$books = explode("\n",$booklist);
for($i = 0, $j = count($books); $i < $j; $i++) {
$book_array[$i]['title'] = substr($books[$i],0,25);
$book_array[$i]['author'] = substr($books[$i],25,14);
$book_array[$i]['publication_year'] = substr($books[$i],39,4);
}
Exploding $booklist into an array of lines makes
the looping code the same whether it's operating
over a string or a series of lines read in from a file.
Once the fields have been parsed into $book_array
by either function, the data can be printed as an HTML table, for
example:
$book_array = pc_fixed_width_unpack('A25title/A14author/A4publication_year',
$books);
print "<table>\n";
// print a header row
print '<tr><td>';
print join('</td><td>',array_keys($book_array[0]));
print "</td></tr>\n";
// print each data row
foreach ($book_array as $row) {
print '<tr><td>';
print join('</td><td>',array_values($row));
print "</td></tr>\n";
}
print '</table>\n';
Joining data on </td><td> produces a
table row that is missing its first <td> and
last </td>. We produce a complete table row
by printing out <tr><td> before the
joined data and </td></tr> after the
joined data.
Both substr( ) and unpack( )
have equivalent capabilities when the fixed-width fields are strings,
but unpack( ) is the better solution when the
elements of the fields aren't just strings.